parallel_fib Parent self spawn fun Parent fib end call left half paral

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
parallel_fib(N) ->
Parent = self(),
spawn(fun () -> Parent ! fib(N - 1) end), % call left half parallel
X = fib(N - 2),
receive Y -> Y end, % get parallel result
X + Y.
fib(N) when N > 20 -> parallel_fib(N);
fib(0) -> 0;
fib(1) -> 1;
fib(N) -> fib(N - 1) + fib(N - 2).
start() ->
io:format("~p~n", [fib(42)]),
init:stop().