Beginner: Having trouble understanding curried function in example

I am currently reading this book because it has challenges I can practice with .

In section 3.6 it states that for the add’ function defined as below

add' :: Int -> (Int -> Int)
add' x y = x + y

“More precisely, add takes an integer x and returns a function, which in
turn takes an integer y and returns the result x + y.”

I am confused on the returns a function part.

Breaking this down
I have add’ which is a function that takes one argument x
When we call it with x it returns another function

Does the function that get return = x + y ?

I guess what is weird to me is that in the code x and y are both supplied to add’ even though when we declared its type we only said it takes on Int.

I’m sure I am overthinking this.

Thanks in advance.

Maybe it is clearer if you write with redundant parentheses:

add' :: Int -> (Int -> Int)
(add' x) y = x + y

Or even more explicitly by using a lambda:

add' x = \y -> x + y

Also note that you can choose to only supply one argument:

xs = map (add' 1) [1,2,3]

That will produce [2,3,4].

1 Like

After looking at your answer for 21 minutes I think I am starting to see some clarity.

The add function takes one integer that returns a function. so

(add' x) 

returns the function of

y = y + x

the function above takes in one argument y and returns the result of x + y .

Is that correct ?

The textual description is correct, but y = y + x is not the right way to define a function that takes one argument y and returns the sum y + x. For that you either have to give it a name, like foo y = y + x or you can use a lambda, which is an anonymous function, like this: \y -> y + x (the \ is an ascii stand-in for the greek lambda letter).

Thank you for the help! The post really helped!

I know this isn’t the correct way to define a function but this helped me break down another example I’ve been working on.
coming from

multThree :: (Num a) => a -> a -> a -> a
multThree x y z = x * y * z

Following the same logic as above I know understand it like the below
assuming we call that function with the numbers 3 , 5 , 9

-- A function returns another function
(multThree 3) y z = 3 * y * z
-- A function returns another function
((mult 3) 5) z = 3  *  5  *  z
-- A function returns a Int
((mult 3) 5) 9 = 3 * 5 * 9

Each function takes one argument and returns a function.

The way that you described it is correct, and in the first chapter of the book Hutton introduces a notation (not Haskell syntax, just a mathematical notation) for breaking down the application of functions to arguments using equational reasoning. Your analysis of how multThree works is exactly what would happen, you just wrote it without using the equational reasoning syntax the book uses. It would be a good exercise to rewrite these curried functions and the process of applying them to arguments using that style, as it is a really useful way to think about Haskell code.

You might also want to check out another recently asked question about currying here. In my answer I went into detail about how the types of curried functions work, but one thing I didn’t mention was that, as a result of function types associating from the right, function application associates from the left. And that’s exactly what you just figured out in your last reply! The question the other OP asked might come up for you as well while you’re thinking about this topic, so I do think you should check out that thread.


Wow. Thank you for the detailed answer!

1 Like