HomeMathComputingArtsWordsLiteratureMusictwitter facebook webfeed

What is Currying in a Computer Language?

Advertise Here For Profit

Xah Lee, 2009-01-28

In Jon Harrop's book 《Ocaml for Scientist》 at http://www.ffconsultancy.com/products/ocaml_for_scientists/chapter1.html

It says:

Currying

A curried function is a function which returns a function as its result.

LOL. That is incorrect.

Here are some examples of a function that returns a function as result, but is not currying.

Mathematica example:

f[n_]:=Function[n^#];
f[7][2]
(* returns 49 *)

Emacs lisp example:

(defmacro f (n) (list 'lambda (list 'x) (list 'expt n 'x) ) )
(funcall (f 7) 2)

Perl example:

sub f {$n=$_[0]; sub { $n ** $_[0]} };
print &{ f(7) } (2);

Javascript example:

function f(n) {return function (x) {return Math.pow(x,n);}; }
alert (f(7) (2));

In the above, a function returns a function, and the result function is applied to a value. They demonstrate 2 things:

These, are 2 of the features that is part of often sloppily termed as “function as first class citizens”.

However, the above are not languages that support currying, which is a feature that Haskell and Ocaml have.

So what is Currying?

Wikipedia article Currying said it best:

In computer science, currying, invented by Moses Schönfinkel and Gottlob Frege, and independently by Haskell Curry,[1] is the technique of transforming a function that takes multiple arguments (or more accurately an n-tuple as argument) in such a way that it can be called as a chain of functions each with a single argument.

Note how it says “is the technique of …”.

To be more concrete, in the context of a given computer language, to say that it support curring, is to mean that the compiler understands the concept to certain degree. More to the point, the language is inherently able to take a function of more than one arg and deconstruct it to several functions of single arg.

To say that function returning function is Currying, is a confusion of fundamental concepts.

Mathematically, currying is the concept of deconstructing a function of multiple parameters to a composition of several functions all of arity 1.

I like Jon, because i consider majority of his argument and perspective are more correct or sensible in his trollish spats in newsgroup fighting with tech geekers. But he is really a ass, and take every chance to peddle his book. Every opportunity, he injects random opinion into discussions about how static typing or greatness of Microsoft, which paves a way for him to post a link to his book on Ocaml/F# or “study” or “speed comparison” of his site. He does this repeatedly and intentionally, about every week for the past 2 or so years, and write in a way to provoke irate responses. In the past 2 or 3 years, i have for 2 or so times without his or any one's solicitation, publicly supported him in ugly newsgroup fights (such as some serious sounding post that accuse him of spamming or or some real life threats about network abuse). However, in the past year as i have had some debates on language issues with jon, i find Jon to be a complete ass as far as his newsgroup demeanor goes.

PS see also: A Mathematica Optimization Problem ( story of a thread where Jon started a fight with me )

blog comments powered by Disqus
2008-12