Skip to content

Instantly share code, notes, and snippets.

@janewang
Created August 4, 2012 17:17
Show Gist options
  • Save janewang/3258829 to your computer and use it in GitHub Desktop.
Save janewang/3258829 to your computer and use it in GitHub Desktop.
YCombinator Factorial in Erlang
Eshell V5.9.1 (abort with ^G)
% The Y combinator allows recursion to be defined as a set of rewrite rules without requiring
% native recursion support in the language.
c(y_comb_single). % takes single param
{ok,y_comb_single}
(y_comb_single:y2(
fun (F) ->
fun(0) -> 1;
(N) -> N * F(N-1)
end
end
))(3).
% --------------
-module(y_comb_single).
-export([y2/1]).
y2(F) ->
(fun (G) -> G(G) end)(
fun (G) ->
F(fun (X) -> (G(G))(X) end)
end
).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment