# Pure functions: currying for & notation

I looked up currying in Shifrin's book.

He points out that currying can be used for pure functions using nested functions as in the following example:

nestedF = Function[x, Function[y, x + y]]

Out: Function[x, Function[y, x + y]]

Out: Function[y$$, 3 + y$$]

Out: 4


The & notation does not allow for this type of currying (it seems) since an expression with two variables #1 and #2 needs two arguments (less than two gives an error). I would like to create a new function having only one variable by applying a function with two variables to a single argument. This is achieved in the above example via add3.

I tend to program using pure functions in the & format rather than the Function format, as this is closer to lambda notation. However when I want to create a function of one variable from a function with two variables (by applying it to an argument) it seems the code requires the Function[ ] use as opposed to the & notation. Is there any way though to still use the # notation for this purpose?

Example:

((#1 + #2) &)[3]


results in the error:

Function::slotn: Slot number 2 in #1+#2& cannot be filled from (#1+#2&)[3].


rather than in the single-argument function

((3 + #2)&)


as one might expect from lambda notation.

It seems from Shifrin I may have to live with this. I wanted to double check anyone knows a way around it so one can use standard lambda contraction behaviour while still in "lambda-notation" (i.e. using # arguments).

There might be some workaround for specific cases, but I suspect that in general you're going to run into ambiguities with the slot notation. The verbose style allows you to avoid those ambiguities.

Another approach is to use SubValues:

curriedAdd[x_][y_] := x + y;
(* 7 *)


This still has the downside that you need to name your arguments. You could also do:

curriedAdd2[x_] := x + # &;
(* 7 *)


But you still have one named argument.

There are some new functions that you might be interested in: CurryApplied and OperatorApplied.

anotherCurriedAdd = CurryApplied[Plus, 2];

f = CurryApplied[#1 + #2 &, 2];

• It's effectively equivalent. Function itself is already a special case. It's an expression that can be applied to arguments. CurryApplied[#1 + #2 &, 2] and CurryApplied[#1 + #2 &, 2][3] are the same--they're just expressions that can be applied to arguments. Jun 9 at 22:05