# Currying

### From HaskellWiki

Currying is the process of transforming a function that takes multiple arguments in a tuple as its argument, into a function that takes just a single argument and returns another function which accepts further arguments, one by one, that the original function would receive in the rest of that tuple.

f :: a -> (b -> c) -- (which can also be written as f :: a -> b -> c )

is the **curried** form of

g :: (a, b) -> c

f = curry g g = uncurry f

Both forms are equally expressive. It holds

f x y = g (x,y) ,

however the curried form is usually more convenient because it allows partial application.

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

*to the right*, so that

*to the left*:

As an illustration, let's take the function

div :: Int -> Int -> Int -- which is actually Int -> (Int -> Int)

But there's more going on here than immediately meets the untrained eye. It could be a two-part process.

On its own,You'll notice that the notation for types reflects this: you can read

*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.

See

- partial application
- 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. - Conversion between curried and uncurried style allows composition of functions with multiple values

## Exercises

- Simplify curry id
- Simplify uncurry const
- Express usingsndorcurryand other basic Prelude functions and without lambdasuncurry
- Write the function without lambda and with only Prelude functions\(x,y) -> (y,x)