【发布时间】:2019-10-14 05:38:22
【问题描述】:
我最近开始从 https://learnyousomeerlang.com 学习 Erlang 在本章errors and processes 中,我了解了程序的作用以及它是如何执行的,但是我无法弄清楚在判断函数中接收语句的目的是什么,何时以及如何调用它?
据我了解,如果元组模式与 Pid 和 atom 匹配,它会返回一个 atom。我将如何发送消息以接收内部法官?
start_critic() ->
spawn(?MODULE, critic, []).
judge(Pid, Band, Album) ->
Pid ! {self(), {Band, Album}},
receive
{Pid, Criticism} -> Criticism
after 2000 ->
timeout
end.
critic() ->
receive
{From, {"Rage Against the Turing Machine", "Unit Testify"}} ->
From ! {self(), "They are great!"};
{From, {"System of a Downtime", "Memoize"}} ->
From ! {self(), "They're not Johnny Crash but they're good."};
{From, {"Johnny Crash", "The Token Ring of Fire"}} ->
From ! {self(), "Simply incredible."};
{From, {_Band, _Album}} ->
From ! {self(), "They are terrible!"}
end,
critic().
输出
c(linkmon).
{ok,linkmon}
Critic = linkmon:start_critic().
<0.109.0>
linkmon:judge(Critic, "Genesis", "The Lambda Lies Down on Broadway").
"They are terrible!"
linkmon:judge(Critic, "Genesis", "A trick of the Tail Recursion").
"They are terrible!"
linkmon:judge(Critic, "Johnny Crash", "The Token Ring of Fire").
"Simply incredible."
【问题讨论】:
标签: functional-programming erlang