Guest User

Untitled

a guest
May 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. -module(echo).
  2. -export([start/0, loop/0, send/2]).
  3.  
  4. start() ->
  5. Pid = spawn(echo, loop, []),
  6. io:format("Started server.~nPid: ~w~n",[Pid]) .
  7.  
  8. loop() ->
  9. receive
  10. {From, Msg} ->
  11. if
  12. is_integer(Msg), Msg >= 0 ->
  13. From ! {self(), "~w",factorial(Msg)},
  14. loop();
  15. Msg == stop ->
  16. From ! {self(), "exiting"},
  17. true;
  18. true ->
  19. From ! {self(), Msg},
  20. loop()
  21. end
  22. end.
  23.  
  24. send(SPid, Msg) ->
  25. Pid = list_to_pid(SPid),
  26. Alive = is_process_alive(Pid),
  27. if
  28. Alive ->
  29. Pid ! {self(), Msg},
  30. receive
  31. {Pid, Msg} ->
  32. io:format("Server is online.~nIt's respond: ~w~n", [Msg])
  33. end;
  34. true ->
  35. io:format("Server is offline.~n")
  36. end.
  37.  
  38. factorial(N) ->
  39. if
  40. N == 0 -> 1;
  41. N > 0 -> N * factorial(N-1)
  42. end.
Add Comment
Please, Sign In to add comment