## `fp::fold` -- create function which iterates over sequences

#### Introduction

`fp::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...>)`

#### Parameters

`f` | - | function
`e` | - | object used as starting value

#### Returns

A function.

#### Details

- `fp::fold` returns a function which repeatedly applies `f` to sequences of arguments, where the expressions `e...` are used as starting values.
- Thus, given the function f and the starting values e[1],...,e[n], `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 2

The 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}}

#### Changes

- no changes

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000