Ranges in action
[1/3] from: jan::skibinski::sympatico::ca at: 29-Oct-2002 3:52
Hi All,
I am attaching a bunch of short examples related
to the range function (..). Nothing clever in it
except that it supports quite economical notation.
Andrew Martin's 'iota function does more or less
the same. These examples have accumulated during
my work on something else.
Regards,
Jan
..[1 4]
; == [1 2 3 4]
; Haskell has it as [1..4] ==> [1,2,3,4]
..[1 3 8]
; == [1 3 5 7] Arithmetical progresion
; in Haskell: [1,3..8] ==> [1,3,5,7]
..[1 1 6]
; [1 1 1 1 1 1] of constant block if step=0
; This version is quite useful. See the bottom.
; I hope it is not confuse anyone
cat [..[1 5] ..[100 110 200]]
; Combining many ranges
map :..[[1 5] [10 110 200]]
; Mapping range operator to produce block of blocks
cat map :..[[1 5] [10 110 200]]
; then concatenating them (rejoin does not do it well)
map :to-money ..[1 10]
; Converting to money
map :to-string ..[1 10]
; or to other objects
map :log-10 ..[1 10]
; or producing logarithmic scales
foldl :+ 0 [1 2 3 4 5]
; Sum of all numbers on the list
foldl :+ 0 ..[1 5]
; Same using range function to define a block
foldl :* 1 ..[1 10]
; Factorial 10
foldl :subtract 0 [1 2 3 4]
; Does not work with, confusion with unary :-
max-block [1 6 3 7 3]
; Picking max numerical values
min-block [1 6 3 7 3]
; or minimum numerical values
poly ..[1 8] 10
; computing polynomials (12345678)
poly ..[1 9] 0.1
; using different bases 9.87654321
poly [1 4 5 6 8 1] 16
; such as hex base (1332865)
scanl :* 1 [1 2 3 4 5]
; list of partial products
scanl :+ 0 .. [1 20]
; list of partial sums
filter :prime? ..[1 20]
; Computing list of prime numbers
; == [3 5 7 11 13 17 19]
filter :prime? (filter :odd? ..[1 20])
; A shorter way
..[1 1 6]
; Constant of six ones [1 1 1 1 1 1]
scanl :* 1 ..[3 3 6]
; Geometrical progression
; [1 3 9 27 81 243 729]
scanl :* 1 ..[2 2 10]
; And another one
;[1 2 4 8 16 32 64 128 256 512 1024]
foldl :+ 0 (scanl :* 1 ..[2 2 10])
; Sum of geom progression == 2047
[2/3] from: joel:neely:fedex at: 29-Oct-2002 6:21
Hi, Jan,
Jan Skibinski wrote:
> Hi All,
> I am attaching a bunch of short examples related
> to the range function (..)...
>
Let me suggest a trip down memory lane... (I can hardly believe
that it was over two years ago! ;-)
http://www.escribe.com/internet/rebol/m2952.html
(I admit that some might view my version of MAP that suppresses
NONE values to be a trick to get two uses from one function!)
Continued discussion on the list (esp. suggestions from Allen and
Andrew) identified some "opportunities" for improvement, so
http://www.escribe.com/internet/rebol/m2959.html
and
http://www.escribe.com/internet/rebol/m2962.html
presented improved versions of the demos.
Interestingly enough, this subject seems to come up about once a
year! The first round of email I have record of was early Oct
of 2000, then again in mid-Oct 2001. It must be Halliotaween!
> max-block [1 6 3 7 3]
> ; Picking max numerical values
>
> min-block [1 6 3 7 3]
> ; or minimum numerical values
>
REBOL has
>> maximum-of [1 6 3 7 3] == [7 3]
>> minimum-of [1 6 3 7 3] == [1 6 3 7 3]
which (as you can see) returns a reference to the series positioned
at the highest/lowest value, so you'll probably see the phrases
first maximum-of ...
and
first minimum-of ...
in some significant portion of their uses.
-jn-
--
; Joel Neely joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]
[3/3] from: jan:skibinski:sympatico:ca at: 29-Oct-2002 9:39
Hi Joel,
> Let me suggest a trip down memory lane... (I can hardly believe that
it
> was over two years ago! ;-)
:-) :-) :-)
So you had your fun too! Maybe it is time for someone here to write
rebol variation on this quite funny and educational article:
http://www.willamette.edu/~fruehr/haskell/evolution.html
I am only pleased to learn that rebol is a "real" language.
Thanks for the pointers! A pity I cannot enjoy the complete
threads since the escribe archive seems to be circularly pointing
back to your original articles.
Jan