samer@9: function X=fold(fn,x) samer@9: % fold - polypmorphic Fold combinator samer@9: % samer@9: % fold :: (X,X->X), Type(A) -> X :- sequence_class(Type). samer@4: % samer@4: % This function applies an associative operator to a list of arguments, samer@4: % as if the list was written out with the operator between consecutive samer@4: % elements, eg fold(@plus,{1,2,3,4}) = 1+2+3+4. samer@9: % samer@9: % Works for any sequence class that has head and next methods, samer@9: % including seq(A) and cells(A). samer@4: samer@9: if isempty(x), X=[]; else X=foldl(fn,head(x),next(x)); end