简体   繁体   中英

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

I am new to Erlang. If I do this

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

Then it is fine. But if I move that function 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM