Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(echo).
- -export([start/0, loop/0, send/2]).
- start() ->
- Pid = spawn(echo, loop, []),
- io:format("Started server.~nPid: ~w~n",[Pid]) .
- loop() ->
- receive
- {From, Msg} ->
- if
- is_integer(Msg), Msg >= 0 ->
- From ! {self(), "~w",factorial(Msg)},
- loop();
- Msg == stop ->
- From ! {self(), "exiting"},
- true;
- true ->
- From ! {self(), Msg},
- loop()
- end
- end.
- send(SPid, Msg) ->
- Pid = list_to_pid(SPid),
- Alive = is_process_alive(Pid),
- if
- Alive ->
- Pid ! {self(), Msg},
- receive
- {Pid, Msg} ->
- io:format("Server is online.~nIt's respond: ~w~n", [Msg])
- end;
- true ->
- io:format("Server is offline.~n")
- end.
- factorial(N) ->
- if
- N == 0 -> 1;
- N > 0 -> N * factorial(N-1)
- end.
Add Comment
Please, Sign In to add comment