# Currying

### From HaskellWiki

(Section of an infix operator) |
(Prelude functions 'curry' and 'uncurry') |
||

Line 1: | Line 1: | ||

[[Category:Glossary]] | [[Category:Glossary]] | ||

− | Currying is the process of transforming a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed. | + | Currying is the process of transforming a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed. |

+ | |||

+ | <haskell> | ||

+ | f :: a -> b -> c | ||

+ | </haskell> | ||

+ | is the '''curried''' form of | ||

+ | <haskell> | ||

+ | g :: (a, b) -> c | ||

+ | </haskell> | ||

+ | You can convert these two types in either directions with the Prelude functions <hask>curry</hask> and <hask>uncurry</hask>. | ||

+ | <haskell> | ||

+ | f = curry g | ||

+ | g = uncurry f | ||

+ | </haskell> | ||

+ | |||

+ | In Haskell, ''all'' functions are considered curried: That is, ''all functions in Haskell take just single arguments.'' | ||

This is mostly hidden in notation, and so may not be apparent to a new Haskeller. Let's take the function <haskell>div :: Int -> Int -> Int</haskell> which performs integer division. The expression <hask>div 11 2</hask> unsurprisingly evaluates to <hask>5</hask>. | This is mostly hidden in notation, and so may not be apparent to a new Haskeller. Let's take the function <haskell>div :: Int -> Int -> Int</haskell> which performs integer division. The expression <hask>div 11 2</hask> unsurprisingly evaluates to <hask>5</hask>. |

## Revision as of 13:03, 3 July 2007

Currying is the process of transforming a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed.

f :: a -> b -> c

is the **curried** form of

g :: (a, b) -> c

f = curry g g = uncurry f

In Haskell, *all* functions are considered curried: That is, *all functions in Haskell take just single arguments.*

div :: Int -> Int -> Int

div 11

*returns a function*of type

Int -> Int

Int -> Int -> Int

*really*saying is "takes an

Much of the time, currying can be ignored by the new programmer. The major advantage of considering all functions as curried is theoretical: formal proofs are easier when all functions are treated uniformly (one argument in, one result out). Having said that, there *are* Haskell idioms and techniques for which you need to understand currying.

Currying provides a convenient way of writing some functions without having to explicitly name them:

- (unsugared:(1+)) is the "increment" function,(+) 1
- is the "double" function,(2*)
- is the "indent" function,("\t"++)
- is the "is-capital-vowel-in-English" function (ignoring the "sometimes Y").(`elem` "AEIOU")

These are examples of partial application (and of a Section of an infix operator).

Sometimes it's valuable to think about functions abstractly without specifically giving all their arguments: this is the Pointfree style.

Sometimes half the work of the function can be done looking only at the first argument (but there really *is* only one argument, remember?): see functional dispatch.