Is it possible to use the @ symbol with multiple arguments? The Prefix
command suggested not. If so, why?
3 Answers
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)
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]] *)
-
$\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 writef[1,2,3]
rather thanf @@ {1,2,3}
?" $\endgroup$ 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 whenf
has an attribute likeHoldAll
. (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$– SzabolcsCommented May 11, 2012 at 7:58 -
$\begingroup$ A simple example illustrating the problems is appreciated. $\endgroup$– EmreCommented May 11, 2012 at 8:03
-
1$\begingroup$ @Emre
i = 1; Table @@ {i, {i, 5}}
Don't think of@@
as a function call. $\endgroup$– SzabolcsCommented 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
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*)
-
$\begingroup$ Well, Wolfram could have made the syntax
f@a,b
orf@(a,b)
legal and equivalent tof[a,b]
. But the former would have been problematic in argument lists (isf[a,g@b,c]
the same asf[a,g[b],c]
or asf[a,g[b,c]]
?). The latter could have been done by just defining(a,b,c)
to be equivalent toSequence[a,b,c]
, however it provides little, if any, benefit to what is currently available. $\endgroup$– celtschkCommented May 11, 2012 at 12:15
f[#, 1, 2] &@x
? $\endgroup$@@
. E.g.:f @@ {x, y, z}
, but that's not prefix anymore.. $\endgroup$@
-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 writef @ (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$