Created
August 4, 2012 17:17
-
-
Save janewang/3258829 to your computer and use it in GitHub Desktop.
YCombinator Factorial in Erlang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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