fp::fold -- create function which
iterates over sequences
Introductionfp::fold(f,e) returns a function which
repeatedly applies f to sequences of arguments, using
e as starting value.
Call(s)fp::fold(f <, e...>)
Parametersf |
- | function |
e |
- | object used as starting value |
ReturnsA function.
Detailsfp::fold returns a function which repeatedly applies
f to sequences of arguments, where the expressions
e... are used as starting values.fp::fold returns the function
which is defined by
(x[1],x[2],...x[m]) -> f(x[m],...f(x[2],f(x[1],e[1],...e[n]))...)for any positive integer m. If the argument sequnce is void (i.e. m=0) the function simply returns the sequence (e[1],...e[n]).
Example
1>> fp::fold(f, x)(y1, y2, y3)
f(y3, f(y2, f(y1, x)))
Example
2The function pset returns the power set of
the set given by its arguments:
>> addelem := (x,y) -> y union map(y, _union, {x}):
pset := fp::fold(addelem, {{}}):
pset(a,b,c)
{{}, {b}, {a}, {c}, {b, c}, {a, b}, {a, c}, {a, b, c}}