8.16.0.1
A package containing many useful algorithms (borrowed from many other programming languages).
There is a GNU Guile scheme version of this package.
Returns a list of elements after apply proc to adjacent elements.
Examples:
> (adjacent-map * '(1 2 3 4 5 6)) |
'(2 6 12 20 30) |
> (adjacent-map < '(1 2 1 3 4 3)) |
'(#t #f #t #t #f) |
This algorithm is similar to Python’s
all.
Returns #t if all the elements of lst are #t, otherwise returns #f.
Examples:
> (all? '(#t #t #t)) |
#t |
> (all? '(#t #t #f)) |
#f |
This algorithm is similar to Python’s
any.
Returns #t if any of the elements of lst are #t, otherwise returns #f.
Examples:
> (any? '(#f #t #f)) |
#t |
> (any? '(#f #f #f)) |
#f |
Takes a binary predicate pred and a list lst, and partitions lst into sublists (chunks) based
on the predicate. The list is split between each pair of adjacent elements for which pred returns #f.
The first element of each such pair belongs to the current chunk, and the second element starts the next chunk.
Examples:
> (chunk-by eq? '(1 1 3 2 2)) |
'((1 1) (3) (2 2)) |
> (chunk-by < '(1 2 1 3 4 3)) |
'((1 2) (1 3 4) (3)) |
Returns a list of lists of k elements each.
Note that this is a specialization of sliding where size is equal to step.
Examples:
> (chunks-of '(1 2 1 3 4 3) 2) |
'((1 2) (1 3) (4 3)) |
> (chunks-of '(1 2 1 3 4 3) 3) |
'((1 2 1) (3 4 3)) |
This algorithm is similar to C++’s
generate.
Returns a list of n elements generated from invoking proc n times.
Examples:
> (generate 3 *) |
'(1 1 1) |
> (generate 3 +) |
'(0 0 0) |
Returns #t if all the elements of lst are strictly increasing,
otherwise returns #f.
Examples:
> (increasing? '(1 2 3 4)) |
#t |
> (increasing? '(1 2 3 5)) |
#t |
> (increasing? '(1 2 2 3)) |
#f |
This algorithm comes from Haskell’s
init.
Return all the elements of a list except the last one.
Examples:
> (init '(1 2 3 4)) |
'(1 2 3) |
> (init '((1 2) (3 4) (5 6))) |
'((1 2) (3 4)) |
This algorithm comes from Clojure’s
juxt.
Takes variable number of procedures and returns a procedure that is the juxtaposition
of those procedures. The returned procedure takes a variable number of arguments, and
returns a list containing the result of applying each procedures to the
arguments.
Examples:
> ((juxt first last) '(1 2 3)) |
'(1 3) |
> ((juxt + *) 1 2 3 4) |
'(10 24) |
> ((juxt zip append) '(1 2 3) '(4 5 6)) |
'(((1 4) (2 5) (3 6)) (1 2 3 4 5 6)) |
Returns the product of the elements in lst.
Examples:
> (product '(1 2 3)) |
6 |
> (product '(1 2 3 4)) |
24 |
This algorithms is the same as Clojures’s
repeat and D’s
repeat.
Returns a list of val repeated n times.
Examples:
> (repeat 5 #t) |
'(#t #t #t #t #t) |
> (repeat 5 '()) |
'(() () () () ()) |
This algorithms is the similar to APL’s
replicate.
Returns a list of of the lst2 values each repeated n times where n is the corresponding element in lst.
Examples:
> (replicate '(1 0 1) '(a b c)) |
'(a c) |
> (replicate '(0 1 2) '(a b c)) |
'(b c c) |
This algorithm originally comes from APL’s monadic
\ scan operator. It is more similar to Haskell’s
scanl1 however.
scanl is similar to
foldl, but returns a list of successive reduced values from the left.
Examples:
> (scanl + '(1 2 3 4)) |
'(1 3 6 10) |
> (scanl * '(1 2 3 4)) |
'(1 2 6 24) |
This algorithm originally comes from APL’s monadic
\ scan operator. It is more similar to Haskell’s
scanr1 however.
scanr is similar to
foldr, but returns a list of successive reduced values from the right.
Examples:
> (scanr + '(1 2 3 4)) |
'(10 9 7 4) |
> (scanr * '(1 2 3 4)) |
'(24 24 12 4) |
Returns a list of lists of size elements each, at offset step apart.
step has to be equal to or smaller than length of the lst.
Examples:
> (sliding '(1 2 3 4) 2) |
'((1 2) (2 3) (3 4)) |
> (sliding '(1 2 3 4 5) 2 3) |
'((1 2) (4 5)) |
> (sliding '(1 2) 2 2) |
'((1 2)) |
Returns #t if all the elements of lst are in sorted order, otherwise returns #f.
Examples:
> (sorted? '(1 2 3 4)) |
#t |
> (sorted? '(1 2 3 3)) |
#t |
> (sorted? '(1 2 3 2)) |
#f |
Returns the sum of the elements in lst.
Examples:
> (sum '(1 2 3 4)) |
10 |
> (sum '()) |
0 |
This algorithm comes from Haskell’s
tail.
Return all the elements of a list except the first one.
Note: this is the same as Racket’s cdr and rest and therefore isn’t really necessary.
Examples:
> (tail '(1 2 3)) |
'(2 3) |
> (tail '(1)) |
'() |
This algorithm is similar to Haskell’s
zip
and Python’s zip.
Returns a list of lists of elements from each of lists passed to the procedure.
Another way to think of zip is that it turns rows into columns, and columns into rows.
This is similar to transposing a matrix.
Examples:
> (zip '(1 2 3) '(4 5 6)) |
'((1 4) (2 5) (3 6)) |
> (zip '() '()) |
'() |
> (zip '(0 1) '(2 3) '(5 7)) |
'((0 2 5) (1 3 7)) |
This algorithm is similar to Haskell’s
zipWith.
Returns a list after zipping together the variadic number of lsts and applying proc
to each of the "zipped together" elements.
Examples:
> (zip-with + '(1 2 3) '(4 5 6)) |
'(5 7 9) |
> (zip-with + '(1 2 3) '(4 5 6) '(7 8 9)) |
'(12 15 18) |