作 w3cschool erlang 的练习
---------
1 -module(tut15). 2 3 -export([start/0, ping/2, pong/0]). 4 5 %% Erlang 消息传递 6 %% Erlang 进程之间的消息可以是任何简单的 Erlang 项. 比如说, 可以是列表、元组、整数、原子、进程标识等等 7 %% recevie 关键字的语法: 请注意, 在 end 前的最后一个 actions 并没有 ";" 8 %% receive 9 %% pattern1 ->10 %% actions1;11 %% pattern2 ->12 %% actions2;13 %% ...14 %% patternN ->15 %% actionsN16 %% end.17 18 ping(0, Pong_PID) ->19 Pong_PID ! finished,20 io:format("ping finished~n", []);21 ping(N, Pong_PID) ->22 Pong_PID ! {ping, self()},23 receive24 pong ->25 io:format("Ping received pong~n", [])26 end,27 ping(N - 1, Pong_PID).28 29 30 pong() ->31 receive32 finished ->33 io:format("Pong finished~n", []);34 {ping, Ping_PID} ->35 io:format("Pong received ping~n", []),36 Ping_PID ! pong,37 pong()38 end.39 40 41 start() ->42 Pong_PID = spawn(tut15, pong, []),43 spawn(tut15, ping, [3, Pong_PID]).
--------- THE END ---------