【问题标题】:What purpose does this receive statement fullfil in this judge function?在这个判断函数中这个接收语句fullfil的目的是什么?
【发布时间】: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


    【解决方案1】:

    Pid ! ... 行向评论家发送消息。然后,critic 将通过From ! ... 行之一发送响应。 judge 函数中的 receive 等待所述响应,然后简单地返回响应中包含的字符串。

    【讨论】:

    • 非常感谢。我现在觉得自己好傻,我怎么会错过。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2017-08-08
    • 2011-02-14
    • 2019-11-04
    • 2021-07-25
    • 2011-04-28
    相关资源
    最近更新 更多