1

The minimal working example of my problem is as follows:

l = {1, 2, 3, 4}
f[a_, b_, c_, d_] = a + b + c + d

Now, I'd like to evaluate

f[l[[1]],l[[2]],l[[3]],l[[4]]]

, but with a syntax like f[Unwrap[l]].

I don't have access to the code of 'f', and I can't simply change the way it is defined to accept a list

Basically, I am missing the functionality present in Python with the *,

l=[0,1,2,3]
def f(a,b,c,d):
    return a+b+c+d
print f(*l)
flebool
  • 187
  • 5

2 Answers2

3

There are two ways of doing this that are mostly equivalent. First,

f[ Sequence @@ l ]
(* 10 *)

But, the use of Sequence is to many characters, in my opinion, and there is a better way. Essentially, the notation @@ is shorthand for the function Apply which replaces the Head of an expression with another head. In the prior case, Sequence replaced the head List which was then passed into f. But, this can be used directly,

f @@ l
(* 10 *)

which replaces the head List with f.

rcollyer
  • 33,976
  • 7
  • 92
  • 191
1

and the third one (thanks to rm -rf)

l = {a, b, c, s};
Operate[f &, l]
f[a, b, c, s]

but in such simple case f@@l is what I use.

Kuba
  • 136,707
  • 13
  • 279
  • 740