-module(mserver2). -import(math_examples,[factorial/1]). -export([start/0,compute_factorial/2]). loop(Count) -> receive stop -> true ; {get_count, From, Ref} -> From ! {result, Ref, Count}, loop(Count); {factorial, From, Ref, N} -> Result = factorial(N), From ! {result, Ref, Result}, loop(Count+1) end. % starting server with initial state 0 start() -> Pid = spawn(fun() -> loop(0) end), register(server,Pid). compute_factorial(Pid, N) -> Ref = make_ref(), Pid ! {factorial, self(), Ref, N}, receive {result, Ref, Result} -> Result end.