Skip to content

Instantly share code, notes, and snippets.

@dce
Created May 10, 2010 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dce/395520 to your computer and use it in GitHub Desktop.
Save dce/395520 to your computer and use it in GitHub Desktop.
-module(fold).
-export([fold/2, fold/3]).
fold([First | Rest], Fun) ->
  fold(Rest, Fun, First);
fold([], Fun) ->
  [].
fold([First | Rest], Fun, Result) ->
  fold(Rest, Fun, Fun(Result, First));
fold([], Fun, Result) ->
  Result.
% 10> fold:fold([1,2,3], fun(Sum, Item) -> Sum + Item end).
% 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment