Hello,everybody! I begin to learn Mathematica. I know /@ means “apply to each element”. so f/@{x} is {f[x]}. But when I try f /@ x, the result is x.I don’t know why. What is the meaning of f /@ x. Would you be kind enough to give me an explanation?
It’s a bit of an edge case. Map[f, expr] applies f to each element of expression at level 1. Note that it works with any expression, not just lists:
In[12]:= Map[g, f[1,2,3]]
Out[12]= f[g[1],g[2],g[3]]
Since x is a symbol, it doesn’t have any parts. Compare to
In[13]:= Map[f,g[]]
Out[13]= g[]
Thank you very much! Inspired by you, I wrote the following words. Are they right?
For g[ ], it has no element at level 1. In other words, we can think that the element of g[ ] at level 1 is “nothing”. When f applies to “nothing”, the result is “nothing”(or just indicated by a backspace). So f/@g[ ] is g[ ].
Actually, your wording is still confusing, because:
g[Nothing]
Out[] = g[Nothing]
I would compare g[Nothing] with List[Nothing] as follows:
f /@ g[Nothing]
Out[]= g[f[Nothing]]
f /@ List[Nothing]
Out[]= {}
The reason for the difference is that List auto-reduces the Nothing to emptiness. Other than that minor difference, List[] and g[] are made indistinguishable when mapping into.
Happy to help!
As @Brad Klee pointed out, Nothing is a pre-defined symbol in Wolfram Language, so your reformulation might still be confusing. I would say something like “the set of elements at level 1 for expression g[] is empty, so applying f to an empty set yields an empty set.”
Thank you very much!
Thanks so much!