# What is a function?

Regarding:

Map[#[{2, b}] &, {Reverse}]


The above works, but it makes my head spin. The documentation for Map says that the first entry is the function, and yet the function of interest (i.e. Reverse) is really inside the second entry.

The first entry #[{2, b}] & is a function because it ends in a &, but the argument of this function is the name of that function (again, Reverse).

• Reverse is a function that is being passed as a parameter value to the pure function #[{2,b}]&. The # (really Slot) will be replaced by the 1st parameter so the pure function becomes Reverse[[{2,b}] and then is evaluated. Functions can be parameters of functions. Apr 22 at 17:17
• Or perhaps Edmund you should say that the name of a function can be a parameter of a function?? Apr 22 at 17:21
• Its not the "name" of the function as it can be a variable assigned to that function. Take foo = Reverse and then replace Reverse with foo in your original expression. Apr 22 at 20:54

A function is really a mathematical idea. In Mathematica, we can represent, or denote, a function in a few ways. But Mathematica is really working with expressions.

Map[a, {b, c, d}]


{a[b], a[c], a[d]}

Mathematica doesn't know that this is meaningless. It's just applying its rules for transforming expressions.

Map[#[x] &, {a, b, c}]


{a[x], b[x], c[x]}

The way Function works (the # and & are just syntactic sugar for Function), is to just replace the argument slots with the values passed in as arguments. And Map does this for every element in a list (well, more accurately, every element at the first level of an expression).

Map[#[x] &, {Sin, Cos, Tan}]


{Sin[x], Cos[x], Tan[x]}

Sin, Cos, and Tan aren't functions, they're just symbols to which evaluation rules are attached. In this case, there are no rules that take us further than just Sin[x] et al, so it stops there and that's the result.

Map[#[{2, b}] &, {Reverse}]


If we could pause evaluation, we could see this: {Reverse[{2, b}]}. But there are rules that still apply to this expression, so we continue on, eventually arriving at {{b, 2}}.

I don't know if you'll find it simple, but try to remember that we apply the concept function to our expressions, but expressions aren't inherently functions. Functions are abstract, but we want to compute as if they're real, so we create expressions that work as if they were functions. This is very useful, but it's just pretending.

• beautifully explained.
– alex
Apr 23 at 12:56
• (++1) Nice, @lericr! Apr 23 at 13:18
Clear["Global*"]

Map[#[{2, b}] &, {Reverse}]

(* {{b, 2}} *)


Or equivalently,

#[{2, b}] & /@ {Reverse}

(* {{b, 2}} *)


While Reverse is a function, in this context the function for Map is #[{2, b}]&. This construct is useful when there are a number of functions to be used and you don't want to repeat the common argument. For example,

#[ExponentialDistribution[λ]] & /@ {Mean, Variance, Median, Skewness,
Kurtosis}

(* {1/λ, 1/λ^2, Log/λ, 2, 9} *)


The function being mapped is #[ExponentialDistribution[λ]]& onto the list of functions {Mean, Variance, Median, Skewness, Kurtosis}

EDIT: The functions being mapped onto need not be only single argument functions.

#[ExponentialDistribution[λ]] & /@ {Mean, Variance, Median, Skewness,
Kurtosis, Moment[#, r] &, CentralMoment[#, r] &}

(* {1/λ, 1/λ^2, Log/λ, 2, 9, λ^-r r!, λ^-r Subfactorial[r]} *)
`