8
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ 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` $\endgroup$ Aug 25, 2012 at 8:35

2 Answers 2

12
$\begingroup$

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}

So you need to read:

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]]]

enter image description here

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

$\endgroup$
5
  • $\begingroup$ 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) $\endgroup$
    – rm -rf
    Aug 22, 2012 at 22:44
  • $\begingroup$ @R.M SubValues (the function) isn't in the documentation, but here's a nice question which covers them. $\endgroup$
    – rcollyer
    Aug 22, 2012 at 22:51
  • $\begingroup$ @rcollyer Right, that and this one are the ones that I remembered $\endgroup$
    – rm -rf
    Aug 22, 2012 at 22:52
  • $\begingroup$ @R.M I had forgotten about that one. $\endgroup$
    – rcollyer
    Aug 22, 2012 at 22:53
  • $\begingroup$ the discussion on currying I think deserves an upvote, but I can't upvote twice $\endgroup$
    – acl
    Aug 22, 2012 at 23:15
6
$\begingroup$

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

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

Mathematica graphics

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.