Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert list to a function arguments in Erlang?

Let's say I have a function of certain arity. And I want to feed it with the corresponding list of numbers.

Obviously, if I have F/2 function and list L = [1,2], I just can do something like this:

F(hd(L), hd(tl(L))).

But how can I make it general? I think, there should be some kind of easy conversion from list to arguments, but I just don't know it yet.

like image 815
akalenuk Avatar asked Dec 20 '22 20:12

akalenuk


1 Answers

Look at the apply/2 function. It does what you want.

> apply( fun (X,Y) -> X+Y end, [1,2]).
3

There is also an apply/3 that includes the module of the function too:

> apply( io, format , [ "~p, ~p", [1,2]]).        
1, 2
like image 130
Diego Sevilla Avatar answered Jan 17 '23 16:01

Diego Sevilla