https://wiki.haskell.org/api.php?action=feedcontributions&user=Slyver.96&feedformat=atomHaskellWiki - User contributions [en]2021-01-27T08:14:19ZUser contributionsMediaWiki 1.27.4https://wiki.haskell.org/index.php?title=Currying&diff=63272Currying2020-03-22T21:03:47Z<p>Slyver.96: </p>
<hr />
<div>[[Category:Glossary]]<br />
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.<br />
<br />
<haskell><br />
f :: a -> (b -> c) -- which can also be written as f :: a -> b -> c <br />
</haskell><br />
is the '''curried''' form of<br />
<haskell><br />
g :: (a, b) -> c<br />
</haskell><br />
You can convert these two types in either directions with the Prelude functions <hask>curry</hask> and <hask>uncurry</hask>.<br />
<haskell><br />
f = curry g<br />
g = uncurry f<br />
</haskell><br />
Both forms are equally expressive.<br />
It holds<br />
<haskell><br />
f x y = g (x,y) ,<br />
</haskell><br />
however the curried form is usually more convenient because it allows [[partial application]].<br />
<br />
In Haskell, ''all'' functions are considered curried: That is, ''all functions in Haskell take just one argument.''<br />
This is mostly hidden in notation, and so may not be apparent to a new Haskeller. It can be said that arrows in the types notation associate ''to the right'', so that <hask>f :: a -> b -> c</hask> is really <hask>f :: a -> (b -> c)</hask>. Functional application, correspondingly, associates ''to the left'': <hask>f x y</hask> is really <hask>(f x) y</hask>, so the types fit. <br><br><br />
<br />
As an illustration, let's take the function <haskell>div :: Int -> Int -> Int -- which is actually Int -> (Int -> Int)</haskell> which performs integer division. The expression <hask>div 11 2</hask> unsurprisingly evaluates to <hask>5</hask>. <br />
<br />
But there's more going on here than immediately meets the untrained eye. It could be a two-part process.<br />
<br />
On its own, <hask>div 11</hask> is a function of type <hask>Int -> Int</hask>. Then that resulting function can be applied to the value <hask>2</hask>, so <hask>(div 11) 2</hask> yields <hask>5</hask>. Of course an optimizing compiler will probably handle that whole expression at once, but conceptually that's what's going on. <br /><br /><br />
<br />
You'll notice that the notation for types reflects this: you can read <hask>Int -> Int -> Int</hask> incorrectly as "takes two <hask>Int</hask>s and returns an <hask>Int</hask>", but what it's ''really'' saying is "takes an <hask>Int</hask> and returns something of the type <hask>Int -> Int</hask>" -- that is, it returns a function that takes an <hask>Int</hask> and returns an <hask>Int</hask>. (One can write the type as <hask>Int x Int -> Int</hask> if you really mean the former -- but since all functions in Haskell are curried, that's not legal Haskell. Alternatively, using tuples, you can write <hask>(Int, Int) -> Int</hask>, but keep in mind that the tuple constructor <hask>(,)</hask> itself can be curried.)<br />
<br />
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. <br />
<br />
See<br />
* [[partial application]]<br />
* [[Section of an infix operator]]<br />
* Sometimes it's valuable to think about functions abstractly without specifically giving all their arguments: this is the [[Pointfree]] style.<br />
* 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]].<br />
* Conversion between curried and uncurried style allows [[Composing functions with multiple values|composition of functions with multiple values]]<br />
<br />
== Exercises ==<br />
<br />
* Simplify <hask>curry id</hask> <!-- (,) --><br />
* Simplify <hask>uncurry const</hask> <!-- fst --><br />
* Express <hask>snd</hask> using <hask>curry</hask> or <hask>uncurry</hask> and other basic Prelude functions and without lambdas <!-- uncurry (flip const) --><br />
* Write the function <hask>\(x,y) -> (y,x)</hask> without lambda and with only Prelude functions <!-- uncurry (flip (curry id)) --></div>Slyver.96