Question

I'm having a hard time understanding notation used for currying. Maybe it means I have a deeper confusion regarding the subject but I don't think so.

I understand how something like...

add (x,y) = x + y can have a type (int, int) -> int.

It also makes sense the curried form would look like this:

add x y = x + y with type int -> int -> int, aka type int -> (int-> int).

I would like to know what the parenthesis mean in that last example. Is the first int x, the second int y, and the last int the output? What would a function like this look like?

(int -> int) -> int

I know something isn't clicking and it's very frustrating.

Was it helpful?

Solution

Types of the form a -> b really just mean

function taking a value of type a and returning a value of type b

And from there on it's just repeated application for larger (multiple arrows) types, much like 1 + 2 + 3 + 4 isn't all that different from 1 + 2. One potential stumbling stone is that either of these values can be a function itself. Your interpretation of the curried type is correct, but note that add x is a valid expression on its own, and gives another function (one which only needs one argument before returning an int).

As for the second type: Just like int -> (int -> int) is the type

take an int and return a function int -> int"

the type (int -> int) -> int means

take a function int -> int and return an int

It's a higher-order function, a function which accepts a function as argument. One not-entirely-useless example would be the function sum0To100 f = sum (map f [0 .. 100]) (or equivalent functions for any other range). For example, sum0To100 fib is the sum of the first 100 Fibonacci numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top