algorithms
algorithms
algorithms
adjacent-
map
all?
any?
chunk-
by
chunks-
of
generate
increasing?
init
juxt
product
repeat
replicate
scanl
scanr
sliding
sorted?
sum
tail
zip
zip-
with
9.1
Racket
top
contents
← prev
up
next →
algorithms
Conor Hoekstra
require
algorithms
package:
algorithms
A package containing many useful algorithms (borrowed from many other programming languages).
There is a GNU Guile scheme
version of this package
procedure
adjacent-map
proc
lst
listof
any/c
proc
->
any/c
any/c
any/c
lst
list?
This algorithm is similar to Haskell’s
mapAdjacent
Returns a list of elements after apply
proc
to adjacent elements.
Examples:
adjacent-map
12
20
30
adjacent-map
#t
#f
#t
#t
#f
procedure
all?
lst
boolean?
lst
listof
boolean?
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
procedure
any?
lst
boolean?
lst
listof
boolean?
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
procedure
chunk-by
pred
lst
list?
pred
->
any/c
any/c
boolean?
lst
list?
This algorithm is similar to C++’s
chunk_by_view
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?
chunk-by
procedure
chunks-of
lst
listof
list?
lst
list?
exact-nonnegative-integer?
This algorithms is the same as Haskell’s
chunksOf
and similar to Python’s
itertools.batched
Returns a list of lists of
elements each.
Note that this is a specialization of
sliding
where
size
is equal to
step
Examples:
chunks-of
chunks-of
procedure
generate
proc
listof
any/c
exact-nonnegative-integer?
proc
->
any/c
This algorithm is similar to C++’s
generate
Returns a list of
elements generated from invoking
proc
times.
Examples:
generate
generate
procedure
increasing?
lst
boolean?
lst
listof
real?
Returns
#t
if all the elements of
lst
are strictly increasing,
otherwise returns
#f
Examples:
increasing?
#t
increasing?
#t
increasing?
#f
procedure
init
lst
list?
lst
list?
This algorithm comes from Haskell’s
init
Return all the elements of a list except the last one.
Examples:
init
init
procedure
juxt
proc
...
procedure?
proc
procedure?
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
juxt
10
24
juxt
zip
append
procedure
product
lst
real?
lst
listof
real?
Returns the product of the elements in
lst
Examples:
product
product
24
procedure
repeat
val
list?
exact-nonnegative-integer?
val
any/c?
This algorithms is the same as Clojures’s
repeat
and D’s
repeat
Returns a list of
val
repeated
times.
Examples:
repeat
#t
#t
#t
#t
#t
#t
repeat
procedure
replicate
lst
lst2
list?
lst
listof
exact-nonnegative-integer?
lst2
list?
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
replicate
procedure
scanl
lst
list?
lst
list?
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
10
scanl
24
procedure
scanr
lst
list?
lst
list?
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
10
scanr
24
24
12
procedure
sliding
lst
size
step
listof
list?
lst
list?
size
exact-nonnegative-integer?
step
exact-nonnegative-integer?
This algorithm is the same as Haskell’s
divvy
, Clojure’s
partition
and D’s
slide
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
sliding
sliding
procedure
sorted?
lst
boolean?
lst
list?
This algorithm is similar to C++’s
std::is_sorted
Returns
#t
if all the elements of
lst
are in sorted order, otherwise returns
#f
Examples:
sorted?
#t
sorted?
#t
sorted?
#f
procedure
sum
lst
real?
lst
listof
real?
Returns the sum of the elements in
lst
Examples:
sum
10
sum
procedure
tail
lst
list?
lst
list?
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
tail
procedure
zip
lst
...
listof
list?
lst
list?
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
zip
zip
procedure
zip-with
proc
lst
...
listof
any/c
proc
->
any/c
...
any/c
lst
list?
This algorithm is similar to Haskell’s
zipWith
Returns a list after zipping together the variadic number of
lst
s and applying
proc
to each of the "zipped together" elements.
Examples:
zip-with
zip-with
12
15
18
top
contents
← prev
up
next →