2

I read a guy's statement on the web saying "currying is just a fancy way of having optional parameters". By gut, I feel that the statement is shallow and wrong but I can't really put my finger on it, probably because I don't have enough knowledge on lambda calculus.

When I try to explain the difference my explanation spans paragraphs, mostly coming down to "Currying is having all variations of a function's declaration with less number of parameters as types. But you cannot have all combinations of a function with optional parameters as valid types and use them in other declarations, at least not automatically".

Is my approach right at the beginning, and more importantly is there a simpler, plainer way to explain it?

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148

1 Answers1

5

It has nothing to do with anything being optional.

But instead of defining a function which takes two parameters, you can define one which takes only one parameter, and returns a function which takes the other parameter.

The end result is the same (the caller ends up providing two parameters), but with currying, you only provide one at a time.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • A nice advantage is the fact that like this all functions take exactly one argument and produce exactly one result. – R. Martinho Fernandes Feb 15 '11 at 18:22
  • 1
    @Martinho, and how is that an advantage? – Sedat Kapanoglu Feb 15 '11 at 23:18
  • @ssg: It's nice from a theoretical perspective, since it makes it easier to reason about programs; and thanks to automatic currying, the user never needs to notice, unless it's to their advantage to notice. – Antal Spector-Zabusky Feb 16 '11 at 05:45
  • 3
    @ssg: a more practical advantage is that it gives you an easy way to specialize functions. Take a function like `map`, for example: a general function which applies *some* operation to every element in a list. With currying, you can supply this "some operation" first, resulting in a specialized function which applies that specific function to every element in a list. Currying makes it very convenient to write extremely generalized and reusable functions, and then specialize them as required for the specific use case. – jalf Feb 16 '11 at 13:23
  • 3
    In OOP, object constructors are kind of a poor mans way to achieve the same: in the constructor you supply some "initial" data, which affects how the member methods on the object will work. It's generally very useful to be able to provide data for a function call in stages, providing general data first, which can be reused across several calls, and then providing the per-call data separately. – jalf Feb 16 '11 at 13:25