Skip to content

Instantly share code, notes, and snippets.

@janewang
Created August 4, 2012 17:21
Show Gist options
  • Save janewang/3258841 to your computer and use it in GitHub Desktop.
Save janewang/3258841 to your computer and use it in GitHub Desktop.
YCombinator List Inversion 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). % 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