5

The @ and /@ functions do the same thing:

Factorial @ List[1, 2, 3, 4, 5, 6]
{1, 2, 6, 24, 120, 720}

Factorial /@ List[1, 2, 3, 4, 5, 6] {1, 2, 6, 24, 120, 720}

What is the / for? I've looked all over and can't find any explanation for it.

Edit:

Using f gives the same result:

f[x_] := x + 1

f @ List[1, 2, 3, 4, 5, 6]
{2, 3, 4, 5, 6, 7}

f /@ List[1, 2, 3, 4, 5, 6] {2, 3, 4, 5, 6, 7}

Dean Schulze
  • 243
  • 1
  • 6

1 Answers1

10

The difference is that for an arbitrary function f,

  • f @ {a,b,c,...} yields f[{a,b,c,...}];
  • f /@ {a,b,c,...} yields {f[a], f[b], f[c], ...}.

The two input forms are equivalent for Factorial because it's what's called a Listable function. For a listable function f, the input f[{a,b,c,...}] automatically evaluates to {f[a], f[b], f[c], ...}. So f @ {a,b,c,...}, in the end, gets you the same thing as f /@ {a,b,c,...}.

To see the difference, look at (for example) the function Max instead. When Max is provided with one or more numbers as input, it returns the largest of those numbers. When it is provided with one or more lists, it returns the largest value in any of the lists. So:

  • Max @ {1,2,3,4,5,6} is equivalent to Max[{1,2,3,4,5,6}]. Since the argument is a list, Mathematica returns the largest value in the list, 6.
  • Max /@ {1,2,3,4,5,6} is equivalent to {Max[1], Max[2], ...}. In each instance, Max is provided with a single number, and so returns that number. So the output is {1,2,3,4,5,6}.
Michael Seifert
  • 15,208
  • 31
  • 68
  • That's the best explanation I've found anywhere, and I've looked. The key is to know that you have to look at the docs for Listable functions, and newcomers to Mathematica won't know that. Unfortunately book authors don't help in that regard. – Dean Schulze Oct 19 '21 at 03:12
  • 3
    Read Leonid shifrin's book. It's free and is a fantastic resource. https://www.mathprogramming-intro.org/ – Lou Oct 19 '21 at 05:10