Nick Mudge Ignition Software Consulting & Development

All functions in Haskell take only one argument. So how do you define functions that take multiple arguments? You don't.

Instead Haskell has currying.

As addition usually takes two arguments (two numbers to add) let's use it as a problem to solve with currying.

A way to think of currying is instead of using one function that takes multiple arguments, use multiple functions that each solve a single argument. Since our version of addition takes two arguments, we are going to have two functions, each taking a single argument.

Here is the currying addition function:

add x = \y -> x + y

There are some interesting things going on here in this code. The whole line, "add x = \y -> x + y", is one function definition called add, but there is another function defined in this line but it doesn't have a name. This part, "\y -> x + y", is a function definition. It could be called a function without a name or an anonymous function or a lambda function. It says that there is one argument (y) and it returns the value from x + y. y is the value from the argument, and the value x comes from outside the function definition, from the x argument for add. Functions that have values defined outside their definition are called closures, so the anonymous function here is a closure.

So what does this line of code do? Let's run it!

add 5 6

Okay, so the first argument is 5. Let's go through this execution in slow motion. Let's put 5 everywhere x is in the function definition:

add 5 = \y -> 5 + y

Okay, since there is nothing left to do here since nothing references what y is so we can't evaluate anything more, we return the value of the function add. The only thing here is the function definition "\y -> 5 + y" so we return that function as the return value of the function add. Let's replace the add function with its return value:

add 5 6
-- we return add 5 and we get:
(\y -> 5 + y) 6

Here we get the anoymous function with the value 6 being passed into it:

(\y -> 5 + y) 6
-- results in:
(\6 -> 5 + 6)
-- which results in:
(5 + 6)

The answer is 11!


Hugo Coneybeer
25 September 2018

The concern and hot topic of these days are technology in which different softwares are installed to start functioning the working of these technologies. As a technology lover, I also needed softwares so I visited and got them best in software development and consultancy.
Will Linton
25 September 2018

<a href="">ABC</a>
Name: (required)
Email: (required)
What has four legs, rhymes with bat and says, "Meow?" (One word answer.)
Spam Filter: