I am new to Mathematica and just experimenting with the different programming constructs. I have been looking at Map
and how to evaluate a function for a list of values and there seems to quite a number of different ways of doing this. I have used Trace
to look at what goes on in more detail but is there any difference in the approaches shown below other than syntax? In my example, both @
and /@
appear to be equivalent to Map
.
f[x_] := x^2;
f /@ {1, 2, 3} // Trace
{f /@ {1, 2, 3}, {f[1], f[2], f[3]}, {f[1], 1^2, 1}, {f[2], 2^2, 4}, {f[3], 3^2, 9}, {1, 4, 9}}
f @ {1, 2, 3} // Trace
{f[{1, 2, 3}], {1, 2, 3}^2, {1^2, 2^2, 3^2}, {1^2, 1}, {2^2, 4}, {3^2, 9}, {1, 4, 9}}
f[{1, 2, 3}] // Trace
{f[{1, 2, 3}], {1, 2, 3}^2, {1^2, 2^2, 3^2}, {1^2, 1}, {2^2, 4}, {3^2, 9}, {1, 4, 9}}
{1, 2, 3} // f // Trace
{f[{1, 2, 3}], {1, 2, 3}^2, {1^2, 2^2, 3^2}, {1^2, 1}, {2^2, 4}, {3^2, 9}, {1, 4, 9}}
f[x_]:=g[x]
instead you'll see a difference. The three last examples are just different notations for the same thing.f@x
isf[a]
isa//f
, inFullForm
they are all represented asf[a]
$\endgroup$Power
has attributeListable
which automatically threads over lists... $\endgroup$