2 Programming Interface

Scheme Procedure: adjacent-map proc lst

Returns a list of elements after apply proc to adjacent elements.

This algorithm is similar to Haskell’s mapAdjacent.

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)
Scheme Procedure: all? lst

Returns #t if all the elements of lst are #t, otherwise returns #f.

This algorithm is similar to Python’s all.

Examples:

> (all? '(#t #t #t))
#t
> (all? '(#t #t #f))
#f
Scheme Procedure: any? lst

Returns #t if any of the elements of lst is #t, otherwise returns #f.

This algorithm is similar to Python’s any.

Examples:

> (any? '(#f #t #f))
#t
> (any? '(#f #f #f))
#f
Scheme Procedure: chunks-of lst k

Returns a list of lists of k elements each.

Note that this is a specialization of sliding where size is equal to step.

This algorithms is the same as Haskell’s chunksOf.

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))
Scheme Procedure: flatten v

Flattens an arbitrary S-expression structure of pairs into a list. More precisely, v is treated as a binary tree where pairs are interior nodes, and the resulting list contains all of the non-null leaves of the tree in the same order as an inorder traversal.

This algorithm is a direct port of racket/base flatten, and is not coming from racket-algorithms package.

Examples:

> (flatten '((a) b (c (d) . e) ()))
'(a b c d e)
> (flatten 'a)
'(a)
Scheme Procedure: generate n proc

Returns a list of n elements generated from invoking proc n times.

This algorithm is similar to C++’s generate.

Examples:

> (generate 3 *)
'(1 1 1)
> (generate 3 +)
'(0 0 0)
Scheme Procedure: increasing? lst

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
Scheme Procedure: init lst

Return all the elements of a list except the last one.

This algorithm comes from Haskell’s init.

Examples:

> (init '(1 2 3 4))
'(1 2 3)
> (init '((1 2) (3 4) (5 6)))
'((1 2) (3 4))
Scheme Procedure: juxt proc

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.

This algorithm comes from Clojure’s juxt.

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))
Scheme Procedure: product lst

Returns the product of the elements in lst.

Examples:

> (product '(1 2 3))
6
> (product '(1 2 3 4))
24
Scheme Procedure: repeat n val

Returns a list of val repeated n times.

This algorithms is the same as Clojures’s repeat and D’s repeat.

Note: this is in fact just alias to make-list and therefore isn’t really necessary.

Examples:

> (repeat 5 #t)
'(#t #t #t #t #t)
> (repeat 5 '())
'(() () () () ())
Scheme Procedure: replicate lst lst2

Returns a list of of the lst2 values each repeated n times where n is the corresponding element in lst.

This algorithms is the similar to APL’s replicate.

Examples:

> (replicate '(1 0 1) '(a b c))
'(a c)
> (replicate '(0 1 2) '(a b c))
'(b c c)
Scheme Procedure: scanl proc lst

Returns a list of successive reduced values from the left.

This algorithm originally comes from APL’s monadic \ scan operator. It is more similar to Haskell’s scanl1 however.

Examples:

> (scanl + '(1 2 3 4))
'(1 3 6 10)
> (scanl * '(1 2 3 4))
'(1 2 6 24)
Scheme Procedure: scanr proc lst

Returns a list of successive reduced values from the right.

This algorithm originally comes from APL’s monadic \ scan operator. It is more similar to Haskell’s scanr1 however.

Examples:

  > (scanr + '(1 2 3 4))
  '(10 9 7 4)
  > (scanr * '(1 2 3 4))
  '(24 24 12 4)
Scheme Procedure: sliding lst size [step 1]

Returns a list of lists of size elements each, at offset step apart.

step has to be smaller than length of the lst.

This algorithms is the same as Haskell’s divvy, Clojure’s partition and D’s slide.

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))
Scheme Procedure: sorted? lst

Returns #t if all the elements of lst are in sorted order, otherwise returns #f.

This algorithm is similar to C++’s std::is_sorted.

Examples:

> (sorted? '(1 2 3 4))
#t
> (sorted? '(1 2 3 3))
#t
> (sorted? '(1 2 3 2))
#f
Scheme Procedure: sum lst

Returns the sum of the elements in lst.

Examples:

> (sum '(1 2 3 4))
10
> (sum '())
0
Scheme Procedure: tail lst

Return all the elements of a list except the first one.

Name of this algorithm comes from Haskell’s tail

Note: this is the same as cdr and therefore isn’t really necessary.

Examples:

> (tail '(1 2 3))
'(2 3)
> (tail '(1))
'()
Scheme Procedure: zip-with proc lst lst

Returns a list after zipping together the variadic number of lsts and applying proc to each of the "zipped together" elements.

This algorithm is similar to Haskell’s zipWith.

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)