为什么spawn()声称我的函数不存在?

时间:2016-03-11 03:06:16

标签: erlang

我正在阅读Learn You Some Erlang一书,并提供以下代码:

-module(fridge).

%% API
-export([start/1]).

start(FoodList) -> spawn(?MODULE, fridge2, [FoodList]).

fridge2(FoodList) ->
  receive
    {From, {store, _Food}} ->
      From ! {self(), ok},
      fridge2([_Food|FoodList]);

    {From, {take, Food}} ->
      case lists:member(Food, FoodList) of
        true ->
          From ! {self(), {ok, Food}},
          fridge2(lists:delete(Food, FoodList));

        false ->
          From ! {self(), not_found},
          fridge2(FoodList)
      end;

    {terminate} ->
      ok
  end.

但是,当我尝试调用start()函数时,出现以下错误:

36> c(fridge).
fridge.erl:8: Warning: function fridge2/1 is unused
{ok,fridge}
37> fridge:start([]).
<0.111.0>

=ERROR REPORT==== 10-Mar-2016::22:02:42 ===
Error in process <0.111.0> with exit value:
{undef,[{fridge,fridge2,[[]],[]}]}

我做错了什么,为什么说这个功能不存在呢?

1 个答案:

答案 0 :(得分:3)

spawn(Module,Function,Args) - &gt; PID()

返回Module:Function to Args应用程序启动的新进程的pid。

所以你必须将你的函数fridge2 / 1像start / 1一样导出为api。

奥利弗这样写:
  start(FoodList) -> spawn(fun() -> fridge2(FoodList) end).