samer@4: function X=fold(fn,args) samer@4: % fold - Fold combinator from functional programming 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@4: % samer@4: % fold :: samer@4: % (X,X->X) ~'associative binary operator', samer@4: % {[N]->X} ~'list (cell array) of arguments (at least 2 elements)' samer@4: % -> X. samer@4: samer@4: if isempty(args), X=[]; else X=foldl(fn,args{1},args(2:end)); end