Skip to content

Instantly share code, notes, and snippets.

@amtal
Created July 21, 2011 19:54
Show Gist options
  • Save amtal/1098051 to your computer and use it in GitHub Desktop.
Save amtal/1098051 to your computer and use it in GitHub Desktop.
Local recursion with access to lexical scope.
-module(iter).
-export([fac/1]).
-define( for(Arg,Seed,Body)
, begin F = fun(Iter,Arg)->Body end, F(F,Seed) end
).
-define( iter(Arg)
, Iter(Iter,Arg)
).
fac(N)->
?for({I,Y}, {N,1},
case (I>1) of
true -> ?iter({I-1,Y*I});
false -> Y
end).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment