Erlang “unbound variable” when calling a function

泪湿孤枕 提交于 2019-12-06 01:17:39

Each Erlang module, as described here, should consist of a sequence of attributes and function declarations, each terminated by period (.)

But this line:

Foo = fun(X) -> X * X end.

... is neither and should be written as follows instead:

foo(X) -> X * X.

foo is lowercase here, because this line is a function declaration, where function name should be an atom.

So in the end your module will look like this:

-module(bakery).
-export([cake/1]).

foo(X) -> X * X.

cake(0) -> [];
cake(N) when N > 0 -> [ foo(2) | cake(N-1) ].

The previous solution is correct, but you can resolve the problem with this code too:

-module(bakery).
-export([cake/1]).

cake(0) -> [];
cake(N) when N > 0 ->
   Foo = fun(X) -> X * X end,
   [ Foo(2) | cake(N-1) ].

Regards.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!