algorithms
adjacent-map
chunks-of
init
product
repeat
scanl
scanr
sliding
sum
tail
zip
zip-with
7.7

algorithms

Conor Hoekstra

 (require algorithms) package: algorithms

A package containing many useful algorithms (borrowed from many other programming languages).

procedure

(adjacent-map lst proc)  (listof any/c)

  lst : (list?)
  proc : (-> any/c any/c any/c)
Returns a list of elements after apply proc to adjacent elements.

This algorithm is similar to Haskell’s mapAdjacent.

procedure

(chunks-of lst k)  (listof list?)

  lst : (list?)
  k : (integer?)
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.

procedure

(init lst)  list?

  lst : (list?)
Return all the elements of a list except the last one.

This algorithm comes from Haskell’s init.

procedure

(product lst)  Real

  lst : (listof Real)
Returns the product of the elements in lst.

procedure

(repeat n val)  list?

  n : (integer?)
  val : (integer?)
Returns a list of val repeated n times.

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

procedure

(scanl lst)  list?

  lst : (list?)
scanl is similar to foldl, but 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.

procedure

(scanr lst)  list?

  lst : (list?)
scanr is similar to foldr, but 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.

procedure

(sliding lst size step)  (listof list?)

  lst : (list?)
  size : (integer?)
  step : (integer?)
Returns a list of lists of size elements each, at offset step apart. Note that step is defaulted to 1.

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

procedure

(sum lst)  Real

  lst : (listof Real)
Returns the sum of the elements in lst.

procedure

(tail lst)  list?

  lst : (list?)
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.

This algorithm comes from Haskell’s tail.

procedure

(zip lst lst2)  (listof list?)

  lst : (list?)
  lst2 : (list?)
Returns a list of corresponding pairs when given two lists.

This algorithm is similar to Haskell’s zip.

procedure

(zip-with proc lst ...)  (listof any/c)

  proc : (-> any/c ... any/c)
  lst : (list?)
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.