2

I noticed that it is possible to construct something like f[x][y]. Displaying this in TreeForm indicates that this expression has head f[x]; and it seems to imply that this is really a function of y, and the name of this function is another function f[x]!

So what are the uses of such constructs? Could x for example be like an index that labels a family functions?

QuantumDot
  • 19,601
  • 7
  • 45
  • 121

1 Answers1

2

One way to think about this syntax is in terms of "functions with parameters that accept inputs". The normal way this is written is

function[inputs..., parameters...]

this structure leads to a somewhat awkward syntax when mapping over a list of inputs:

list = {1.234, 5.678};
(Round[#1, 0.1] & ) /@ list

(* {1.2, 5.7} *)

The two-argument-list syntax allows you to construct a parameterized operator that acts upon inputs:

round[a_][x_] := Round[x, a];
round[0.1] /@ list

(* {1.2, 5.7} *)

which is a little tidier. I believe the explosion of built-in functions which permit this syntax is due to the new Query function in version 10:

Query[round[0.1]][list]

(* {1.2, 5.7} *)

In general, there is a lot more support for more formal functional programming style in version 10.

Daniel W
  • 3,416
  • 18
  • 31