Skip to content

Instantly share code, notes, and snippets.

@adolfont
Created June 7, 2022 12:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adolfont/4aeace3570a6678107393afb450b2c2b to your computer and use it in GitHub Desktop.
Save adolfont/4aeace3570a6678107393afb450b2c2b to your computer and use it in GitHub Desktop.
Source code for Joe Armstrong's My favorite Erlang Program https://joearms.github.io/published/2013-11-21-My-favorite-erlang-program.html
-module(fav1).
-export([test/0]).
test() ->
Pid = spawn(fun universal_server/0),
Pid ! {become, fun fac_server/0},
Pid ! {self(), 50},
receive
X -> X
end.
universal_server() ->
receive
{become, F} -> F()
end.
fac_server() ->
receive
{From, N} ->
From ! factorial(N),
fac_server()
end.
factorial(0) -> 1;
factorial(N) -> N * factorial(N-1).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment