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

I am new to Erlang. If i do it

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

Then it’s wonderful. But if I moved this function to a module, it will say "Illegal expression". For example, this

 H = misc_functions:square. 

Please, help.

+6
module erlang
source share
2 answers

Erlang function references require the fun and arity keywords. Suppose square accepts one parameter, the correct assignment:

 H = fun misc_function:square/1 
+17
source share

You can also do something like this:

 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.

+2
source share

All Articles