# What is the difference between Composition (@*) and sequential applications (@)?

What is the difference between

f@*g@*h@x


and

f@g@h@x


Both evaluate to

f[g[h[x]]]


If they're the same, why introduce Composition as a new feature?

• Related: (54762) – Mr.Wizard Apr 4 '15 at 0:04
• Observe the difference between h = f @ g; h[x] and h = f @* g; h[x]. – David Zhang Apr 4 '15 at 15:42

Clearly the @ notation is inspired by the usual mathematical notation for function composition. f@g[x] looks very similar to the mathematical notation $(f\circ g)(x)$. But it is important to understand that @ does not denote function composition. In mathematical notation $f\circ g$ is also a function. In Mathematica f@x is simply a different way to write f[x], but f@g is not (generally) a function. Both f@x and f[x] parse to the exact same Mathematica expression.

So what is the true equivalent of $f \circ g$? It is Composition[f, g] which can be more concisely written as f @* g since version 10, and can be used in situations where we need a function without applying it to an argument (e.g. with Map, or as an operator with Dataset).

Both of your examples evaluate to the very same things in the end, so they behave equivalently in this case. But the way Mathematica arrives to the same end result is different:

f@g@h@x parses to an expression with the FullForm f[g[h[x]]], which doesn't evaluate further. There's no evaluation step.

f@*g@*h@x parses to an expression with the full form Composition[f,g,h][x], which then evaluates to f[g[h[x]]].

It's also worth pointing out that @ and @* have different precedences and associativity properties as operators. f@g@x is equivalent to f@(g@x) and f@*g@x is equivalent to (f@*g)@x. Writing ...[...] has a different precedence again so f@*g[x] is the same as f@*(g[x]) (i.e. it's not the same as f@*g@x).

• Could you say a bit more about the different meanings? – orome Apr 3 '15 at 15:24
• Just to make it more clear, the big difference is that f @ g is the same as f[g], while f @* g is roughly equivalent to f[g[#]] &. For example of usage, StringLength @* ToString /@ Range works whereas StringLength @ ToString /@ Range does not. – 2012rcampion Apr 3 '15 at 15:25
• That's why I added "roughly equivalent." and changed the first "equivalent" to "is the same," to make clear the differences. But if you don't know the details about Composition it gets the point of what it does across. – 2012rcampion Apr 3 '15 at 15:30
• @Szabolcs: So is it fair to say that the (or at least a) key conceptual distinction is that @* creates a function (which is not what @ does)? – orome Apr 3 '15 at 17:14
• @raxacoricofallapatorius Yes. That is correct. – Szabolcs Apr 3 '15 at 17:14

... why introduce Composition as a new feature?

Composition is used to create a new anonymous function that can be used in all the standard ways such as Map and Apply etc. To achieve the same thing without it one needs a Function. Much like operator forms the use of Composition allows one to eliminate extraneous Function constructs which can make code more linear and easier to read. The short form @* and its companion /* (see RightComposition) make its application pleasingly concise.

Composition is not typically used for a single application but instead when one wishes to perform an operation multiple times. Here are some related form:

f@*g@*h /@ {x, y, z}
f@g@h@# & /@ {x, y, z}
f /@ g /@ h /@ {x, y, z}


Each of these evaluate the same way in this particular case but there are differences that are not immediately apparent. We can see some of them if we define:

f = HoldForm; g = Print; h = Sqrt;


Now:

f@*g@*h /@ {x, y, z}
f@g@h@# & /@ {x, y, z}
f /@ g /@ h /@ {x, y, z} Because Composition does not hold its arguments the Symbols g and h are resolved to Print and Sqrt in the output from the first line. In the second line f resolves first to HoldForm and as a result g and h remain verbatim. Finally in the last line each function application is evaluated sequentially therefore the Print fires and Null is returned as the actual output.