How to convert list to a function arguments in Erlang?

erlang, function, list

Solution

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

Problem

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.

Original source