# Keywords to find documentation for this syntax

Mathematica allows this syntax, but I cannot find any documentation (or books) that discuss the various uses/advantages of it

g[1] := Plus;
g[2] := Times;
g[1][3, 5]
(* 8 *)

g[2][3, 5]
(* 15 *)


What keywords can I use to find this in the documentation?

• Welcome to Mathematica.SE! Let me remind you three things we usually do here: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign Aug 25, 2012 at 8:35

Your g[1] and g[2] are simply acting as Head:

g[1] := Plus;


So there is no mystery in this syntax:

 {g[1][a, b], Plus[a, b]}


{a + b, a + b}

 Head /@ {g[1][a, b], Plus[a, b]}


{Plus, Plus}

But maybe there is a bit more to it than meets the eye. You actually almost wondered into programming concept called Currying which according to Wikipedia "is the technique of transforming a function that takes multiple arguments in such a way that it can be called as a chain of functions each with a single argument (partial application)."

So you can do things like:

f[x_][y_] := Sin[x y]

f[x] /@ Range[5]


{Sin[x], Sin[2 x], Sin[3 x], Sin[4 x], Sin[5 x]}

Documentation mentions it here. For deeper insight see discussion in @SalMangano "Mathematica Cookbook".

BTW, @acl nice addition (to see how Mathematica thinks) can be visualized as

TreeForm[Trace[g[1][a, b]]]


So the thinking goes from top to bottom and from left to right.

• If you're mentioning Currying, then you might add in a word or two and some links to SubValues (I remember there were a few posts here too)
– rm -rf
Aug 22, 2012 at 22:44
• @R.M SubValues (the function) isn't in the documentation, but here's a nice question which covers them. Aug 22, 2012 at 22:51
• @rcollyer Right, that and this one are the ones that I remembered
– rm -rf
Aug 22, 2012 at 22:52
• @R.M I had forgotten about that one. Aug 22, 2012 at 22:53
• the discussion on currying I think deserves an upvote, but I can't upvote twice
– acl
Aug 22, 2012 at 23:15

Just to add to Vitaliy's answer: You can see what happens with

g[1] := Plus;
FullForm /@ (g[1][3, 4] // Trace)


So, on evaluating g[1][3,4], Mathematica looks up g[1] and sees it evaluates to Plus; it's then left with Plus[3,4] which evaluates to 7`.

• +1 Cool idea to use Trace. I referred to your answer too ;-) Aug 22, 2012 at 22:41
• @VitaliyKaurov does that mean the entire fabric of reality is going to rip apart? Or, just stackexchange? Aug 22, 2012 at 22:52
• @rcollyer This just means that you need to answer too and do a three way cross-referencing
– rm -rf
Aug 22, 2012 at 23:00
• @R.M there's a French term for that, but I don't think it is appropriate for polite company. Aug 23, 2012 at 0:09
• @rcollyer it just means household of three so I don't see the problem :)
– acl
Aug 23, 2012 at 0:12