RM777
RM777

Reputation: 203

Currying and Brackets in function calls

I have a hard time to understand Currying in Haskell. I was not able to understand the already existing answers to this topic. I want to understand what the difference is between:

(Int -> Int) -> Int

and

Int -> (Int - > Int)

I think the answer has something to do with currying. I have already consulted some resources about the topic and I have got an idea.

Functions that have several parameters can be described as an array of elementary operations with one input and one output.

According to the online tutorial learnyouahaskell:

"Doing max 4 5 first creates a function that takes a parameter and returns either 4 or that parameter, depending on which is bigger. Then, 5 is applied to that function and that function produces our desired result."

So in this case the elementary basic operations are just comparing two values. And the first operation is

if "lowest value possible" > 4 then "lowest value possible" otherwise 4

The second operation is

if 4 > 5 then 4 otherwise 5

This would represent Int -> (Int -> Int) but what would (Int -> Int) -> Int be?

Upvotes: 0

Views: 127

Answers (2)

fp_mora
fp_mora

Reputation: 714

The idea of partial function application is a hallmark of Haskell. Exploiting partial application is natural. It is much how we think. I fill my glass half with tea. Then I fill it completely with lemonaid.

m2 = max 4 --partial application (Int -> Int)

m2 5 -- full application (Int -> Int) -> Int

All functions in Haskell are curried. It is sometimes necessary to disable it with uncurry

m3 = uncurry max

m4 = m3 4 -- does not become (Int -> Int)

m4 3 -- is not (Int -> Int) -> Int -> Int

Does not work and the error has to do with partial application (Int -> Int) because the function now requires two parameters.

Upvotes: 0

chi
chi

Reputation: 116139

Int -> (Int -> Int)

is the same type as

Int -> Int -> Int

and is the type of a curried two-arguments function, both being integers. That is it is a function which takes an integer argument and returns a function, whose argument is another integer, and which finally returns an integer.

Possible calls are f 3 2 and f (7+4) (5*8).

Possible definitions are f a b = a+b and f a b = a*a+42*b.

Instead,

(Int -> Int) - > Int

is not a binary function, curried or not. It is a function with a single argument, and this argument is a function Int -> Int. The final result is an integer.

Possible calls are f (\x -> x+1), f id, and f g where g n = n*4+5.

Possible definitions are f h = h 45 and f h = h (h 6) + h 7 + 9.

Upvotes: 2

Related Questions