annotate general/funutils/fold.m @ 37:beb8a3f4a345
Renamed prefs to options throughout.
author |
samer |
date |
Mon, 28 Jan 2013 10:52:11 +0000 |
parents |
45aaf9b2d7b0 |
children |
|
rev |
line source |
samer@9
|
1 function X=fold(fn,x)
|
samer@9
|
2 % fold - polypmorphic Fold combinator
|
samer@9
|
3 %
|
samer@9
|
4 % fold :: (X,X->X), Type(A) -> X :- sequence_class(Type).
|
samer@4
|
5 %
|
samer@4
|
6 % This function applies an associative operator to a list of arguments,
|
samer@4
|
7 % as if the list was written out with the operator between consecutive
|
samer@4
|
8 % elements, eg fold(@plus,{1,2,3,4}) = 1+2+3+4.
|
samer@9
|
9 %
|
samer@9
|
10 % Works for any sequence class that has head and next methods,
|
samer@9
|
11 % including seq(A) and cells(A).
|
samer@4
|
12
|
samer@9
|
13 if isempty(x), X=[]; else X=foldl(fn,head(x),next(x)); end
|