[REBOL] Ranges in action
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