Erlang (programming language)/Tutorials/Folding

From Citizendium
< Erlang (programming language)‎ | Tutorials
Revision as of 13:36, 13 September 2008 by imported>Eric Evers (New page: Fun with folding Fold is a powerful tool, when you get to know it. lists:foldr(F,S,L) takes three arguments: F is some folding function, S is some starting value, and L is some List that...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Fun with folding

Fold is a powerful tool, when you get to know it. lists:foldr(F,S,L) takes three arguments: F is some folding function, S is some starting value, and L is some List that needs folding. Let us do some simple folding. The following fold calculates the lenght of a list. Here the current list item, _C is ignored, and the accumulator, A, counts the number of elements in the list.

 lists:foldr( fun(_C,A)->A+1 end, 0, [1,2,3]).
 3