Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is it possible to use the @ symbol with multiple arguments? The Prefix command suggested not. If so, why?

share|improve this question
    
Umm... f[#, 1, 2] &@x? –  rm -rf May 11 '12 at 7:04
    
I see only one argument there: x. The others are tucked away, so to speak. –  Emre May 11 '12 at 7:10
    
Well, then you're probably looking for @@. E.g.: f @@ {x, y, z}, but that's not prefix anymore.. –  rm -rf May 11 '12 at 7:12
    
Indeed. You can make that an answer. –  Emre May 11 '12 at 7:14
    
I believe the @-syntax was inspired by the function composition operator which is often denoted with a circle in mathematics: $(f\circ g)(x) = f(g(x))$. It seems natural to restrict it to one argument. If you write f @ (x,y), it suggests that (x,y) stands as a unit by itself, which is not the case. Of course we can just guess why exactly multiple argument were excluded when the syntax was defined. –  Szabolcs May 11 '12 at 8:01

3 Answers 3

up vote 11 down vote accepted

If you use Sequence instead of List for listing the multiple arguments, then @ also works:

f@Sequence[x, y, z]
(* ==> f[x, y, z]  *)

or

f@({x, y, z}/.List->Sequence)
share|improve this answer
    
+1 Technically this is the correct answer. –  Ajasja May 11 '12 at 9:33

In order to supply multiple arguments the way you intend, you should use Apply. For example,

f @@ {x, y, z}
(* f[x, y, z] *)

I hesitate to suggest this as a "supply multiple arguments" function, because all that Apply does is to replace the Head of the expression. It so happens that here this has the same effect that you desire. However, if you remember it this way, it could lead to a conceptual block when you try to think of using Apply at different levels.

Another important point that I forgot to mention, and Szabolcs points out below, is that this will give you different results if the function has hold attributes (another reason not to think of it as mentioned above). For example:

SetAttributes[f, HoldAllComplete]
f @@ {a, b, Sequence[c, d]}
f[a, b, Sequence[c, d]]

(* f[a, b, c, d]
   f[a, b, Sequence[c, d]] *)
share|improve this answer
    
+1 but I cannot agree with the last sentence; I "remembered" it that way when I learned @@ and I found @@@ a natural and useful extension, all before the time I really understood or at least thought in terms of expression replacement. In fact I think I started using @@@ first, because in that primitive mindset: "why wouldn't I just write f[1,2,3] rather than f @@ {1,2,3}?" –  Mr.Wizard May 11 '12 at 7:57
1  
It should be noted though that this behaves differently from f[x,y,z] in some cases, for example when f has an attribute like HoldAll. (Just to make it even more clear that this is not an alternate syntax for a function call. It's a different thing that behaves similarly.) –  Szabolcs May 11 '12 at 7:58
    
A simple example illustrating the problems is appreciated. –  Emre May 11 '12 at 8:03
1  
@Emre i = 1; Table @@ {i, {i, 5}} Don't think of @@ as a function call. –  Szabolcs May 11 '12 at 8:04
    
@Mr.Wizard Well... :) I changed the wording to "could lead to..." rather than "definitely", which was a little too strong. –  rm -rf May 11 '12 at 8:15

I don't think so: you would have a lot of ambiguity as to what are the arguments. Also, how would you write the shorthand version?

Lets take for example Part[list,i]

Part@list@i Is no good (applying list to i)

Part@list,i Is not valid syntax...

If your other arguments don't change much you could write wrapper functions with the other arguments given. For example:

Second[l_] := Part[l, 2];
l = {1, 2, 3};
Second@l
(*2*)
share|improve this answer
    
Well, Wolfram could have made the syntax f@a,b or f@(a,b) legal and equivalent to f[a,b]. But the former would have been problematic in argument lists (is f[a,g@b,c] the same as f[a,g[b],c] or as f[a,g[b,c]]?). The latter could have been done by just defining (a,b,c) to be equivalent to Sequence[a,b,c], however it provides little, if any, benefit to what is currently available. –  celtschk May 11 '12 at 12:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.