开发者

How to assign Function from a module to a variable in Erlang?

开发者 https://www.devze.com 2023-02-02 05:37 出处:网络
I am new to Erlang. If I do this H = fun(X) -> X*X. Then it is fine. But if I move that function开发者_StackOverflow to a module, it says \"Illegal Expression\". For example this

I am new to Erlang. If I do this

H = fun(X) -> X*X.

Then it is fine. But if I move that function开发者_StackOverflow to a module, it says "Illegal Expression". For example this

H = misc_functions:square.

Please help.


Erlang function references require the keyword fun and the arity. Suppose that square takes a single parameter, the correct assignment is:

H = fun misc_function:square/1


You can also do something like that:

1> F = fun(X) -> misc_function:square(X) end.
#Fun<erl_eval.6.13229925>
2> F(4).
16
3>

Defining a function that calls inside your desired function.

0

精彩评论

暂无评论...
验证码 换一张
取 消