Currying in Haskell (With Some JavaScript)

Nested Software - May 2 '18 - - Dev Community

I've been delving a bit into the functional language Haskell recently, and I discovered that it has a somewhat unusual way of handling function parameters. Usually, you supply the arguments and call a function, and that's the end of the story.

For example, the following trivial JavaScript sub function just subtracts its two arguments:

const sub = (first, second) => first - second
Enter fullscreen mode Exit fullscreen mode

We can call it as follows:

sub(7,2)
Enter fullscreen mode Exit fullscreen mode

Let's write sub in Haskell and find out how it is different from the JavaScript version:

main = print (sub 7 2) 

sub :: (Num a) => a -> a -> a
sub first second = first - second
Enter fullscreen mode Exit fullscreen mode

Let's see the result:

C:\dev>ghc sub.hs
[1 of 1] Compiling Main             ( sub.hs, sub.o )
Linking sub.exe ...
C:\dev>sub.exe
4
Enter fullscreen mode Exit fullscreen mode

This looks as though it's the same function. The signature seems to be saying: Take two numbers as parameters and return a third number as a result. However, notice how there are no parentheses in a -> a -> a? One might expect something more like (a, a) -> a. That's actually a clue that something slightly different is going on.

Below I've tried to come up with a way to show this:

main = print finalresult
    where finalresult = partialresult 3
          partialresult = sub 7
Enter fullscreen mode Exit fullscreen mode

If we modify the main function as above, we can see that calling sub with just a single argument, 7, returns a function. We call this intermediate function with 3, which then returns 4, the actual result of the subtraction.

Each time a function is returned, it retains access to the parameters that were passed in to its calling function. Functions that can retain enclosing scope like this, even once execution has moved out of the block associated with that scope, are called closures.

So, what is really happening then? In fact, the sub function takes a single number as a parameter and returns a function. That function also takes a number as a parameter, and returns the result of the subtraction. This idea of decomposing a function that takes multiple arguments into a nesting of functions where each function just has one argument is called currying.

Let's try to simulate this behaviour with JavaScript:

const sub = first => {
    const intermediateResult = second => {
        return first - second
    }

    return intermediateResult
}
Enter fullscreen mode Exit fullscreen mode

Here's how we'd call this type of function in JavaScript:

const result = sub (7) (3) 
console.log('subtraction result = ' + result)
Enter fullscreen mode Exit fullscreen mode

We call sub with 7 as an argument and then we call the function that it returns with 3. This intermediate function is the one that actually computes the difference between the two values.

In Haskell, currying is built into the language. Any function in Haskell can be called with partial arguments, and the remaining arguments can be applied later.

Is currying useful?

map (+3) [1,5,3,1,6]
Enter fullscreen mode Exit fullscreen mode

In Haskell we can just call the + function with a single argument, 3 in this case. map then then calls the intermediate function with each of the items in the list as parameters.

In something like JavaScript, we can't do this directly, but we can get around the problem easily enough with a lambda:

[1,5,3,1,6].map(x=>x+3)
Enter fullscreen mode Exit fullscreen mode

While currying doesn't strike me as being essential to functional programming in the same way that the concepts of first-class functions and closures are, I have to admit there is a certain orthogonality and conceptual purity to the way Haskell handles arguments.

In particular, currying fits in well with the fact that most everything in Haskell is evaluated lazily. In that context currying makes some sense, since most functions evaluate to a thunk anyway and the underlying logic is not fully processed until a complete result is required.

If you're interested in learning more about Haskell, I highly recommend getting started with the tutorial Learn You a Haskell for Great Good!.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .