8.6
algorithms
A package containing many useful algorithms (borrowed from many other programming languages).
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 |
This algorithms is the same as Haskell’s
chunksOf.
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)) |
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 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.
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)) |
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.
Returns a list of corresponding pairs when given two lists.
Examples:
| > (zip '(1 2 3) '(4 5 6)) |
| '((1 4) (2 5) (3 6)) |
| > (zip '() '()) |
| '() |
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) |