6
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ Umm... f[#, 1, 2] &@x? $\endgroup$
    – rm -rf
    Commented May 11, 2012 at 7:04
  • $\begingroup$ I see only one argument there: x. The others are tucked away, so to speak. $\endgroup$
    – Emre
    Commented May 11, 2012 at 7:10
  • $\begingroup$ Well, then you're probably looking for @@. E.g.: f @@ {x, y, z}, but that's not prefix anymore.. $\endgroup$
    – rm -rf
    Commented May 11, 2012 at 7:12
  • $\begingroup$ Indeed. You can make that an answer. $\endgroup$
    – Emre
    Commented May 11, 2012 at 7:14
  • 1
    $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Commented May 11, 2012 at 8:01

3 Answers 3

14
$\begingroup$

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)
$\endgroup$
0
8
$\begingroup$

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]] *)
$\endgroup$
6
  • $\begingroup$ +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}?" $\endgroup$
    – Mr.Wizard
    Commented May 11, 2012 at 7:57
  • 1
    $\begingroup$ 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.) $\endgroup$
    – Szabolcs
    Commented May 11, 2012 at 7:58
  • $\begingroup$ A simple example illustrating the problems is appreciated. $\endgroup$
    – Emre
    Commented May 11, 2012 at 8:03
  • 1
    $\begingroup$ @Emre i = 1; Table @@ {i, {i, 5}} Don't think of @@ as a function call. $\endgroup$
    – Szabolcs
    Commented May 11, 2012 at 8:04
  • $\begingroup$ @Mr.Wizard Well... :) I changed the wording to "could lead to..." rather than "definitely", which was a little too strong. $\endgroup$
    – rm -rf
    Commented May 11, 2012 at 8:15
3
$\begingroup$

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*)
$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – celtschk
    Commented May 11, 2012 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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