Created
August 4, 2012 17:21
-
-
Save janewang/3258841 to your computer and use it in GitHub Desktop.
YCombinator List Inversion 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). % takes 2 params | |
{ok,y_comb} | |
(y_comb:y2( | |
fun (R) -> | |
fun | |
([H|T], L) -> R(T, [H|L]); | |
([], L) -> L | |
end | |
end | |
))(" suolfillem", "music"). | |
"melliflous music" | |
% -------------- | |
-module(y_comb). | |
-export([y2/1]). | |
y2(F) -> | |
(fun (G) -> G(G) end)( | |
fun (G) -> | |
F(fun (X, Y) -> (G(G))(X, Y) end) | |
end | |
). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment