Skip to content

Instantly share code, notes, and snippets.

@bryanhunter
Created December 16, 2013 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanhunter/7994399 to your computer and use it in GitHub Desktop.
Save bryanhunter/7994399 to your computer and use it in GitHub Desktop.
Joe Armstrong's favorite Erlang program (from http://joearms.github.io/2013/11/21/My-favorite-erlang-program.html)
-module(demo1).
-export([test/0]).
universal_server() ->
receive
{become, F} ->
F()
end.
factorial_server() ->
receive
{From, N} ->
From ! factorial(N),
factorial_server()
end.
factorial(0) -> 1;
factorial(N) -> N * factorial(N-1).
test() ->
Pid = spawn(fun universal_server/0),
Pid ! {become, fun factorial_server/0},
Pid ! {self(), 50},
receive
X -> X
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment