Friday, 29 April 2016

Haskell: Currying

Currying is a process of transforming multi argument functions into a function that takes just a single argument and returns another function if any arguments are still needed, named for the British mathematician and logician Haskell Curry. For example, a function that takes two parameters is actually two one-parameter functions. .In Haskell, all the functions are implemented using currying, that means all the functions in Haskell just take one argument.

Why currying is required in Haskell?
To make the use of partial applications easy, Haskell provides currying. In Partial applications, we can take a function with multiple arguments, apply it to some of the arguments and get a function with remaining arguments.


For Example,
*Main> let sum a b = (a + b)
*Main> let incBy1 = sum 1
*Main> 
*Main> incBy1 10
11
*Main> incBy1 24
25
*Main> 
*Main> let decBy1 = sum (-1)
*Main> 
*Main> decBy1 10
9

sum a b = (a + b)
Above snippet define function ‘sum’ which add two numbers.

incBy1 = sum 1
Above is the partial application.


incBy1 10 = sum 1 10 = 11, incBy1 is replaced by sum 1.



Previous                                                 Next                                                 Home

No comments:

Post a Comment