is a style of treating multiple-argument functions as single-argument functions, named after the logician Haskell Curry
, though it was invented by Moses Schönfinkel. A curried function typically consumes the first argument evaluating to another function, which consumes the second argument, evaluating to ... and so on, until the last argument is consumed, evaluating to the result. For example, using lambda calculus
notation, a curried
sum function is λ x . λ y . x + y; the λ-expression (λ x . λ y . x + y) 3 4 is first β-reduced to (λ y . 3 + y) 4 and then β-reduced to 3 + 4, which is then δ-reduced to 7.
Currying is popular in functional programming
; it is contrasted by the non-currying style favoured by mathematicians
, where multiple arguments are converted to a single argument by packing the arguments into a tuple that forms the single argument.
In functional programming languages, currying is an operation performed on functions of more than one argument. Currying a function f of two arguments produces a function g of one argument that returns a function of one argument such that f(x, y) equals (g(x))(y), or in Lisp notation
(f x y) equals
((g x) y). By extension, fully currying a function f of three arguments produces g such that f(x, y, z) equals ((g(x))(y))(z), or in Lisp notation
(f x y z) equals
(((g x) y) z).
To do currying in the Scheme programming language:
(lambda (x) ; take the first argument
(lambda y ; and the rest of the args as a list
(f x . y)))))
If g equals
(f x y)
((g x) y)
(f x y z)
((g x) y z)
These languages automatically fully curry functions called with too few arguments: