simplesystems
simplesystems

Reputation: 849

Haskell: How to recognize a curried function?

Given the following two functions:

minCur a = (\b -> if a > b then b else a)

minCur a b = if a > b then b else a

Asking for the type results for both the same:

minCur :: Ord p => p -> p -> p

How does the User of the function know if he is dealing with a curried function? Does it even matter?

Edit:

Is this currying?

minCur (a,b) = if a > b then b else a

Upvotes: 1

Views: 579

Answers (2)

Edward Minnix
Edward Minnix

Reputation: 2937

It does not matter, since every function that takes more than one argument is a curried function. In Haskell, every function takes a single parameter and returns a single value (a -> b -> c is the same as a -> (b -> c)).

There shouldn't be a reason for the user to care whether the function is curried or not.

The two function definitions you provided are considered to be the same function. The user shouldn't need to care beyond what the behavior of minCur is and what type it has.

In the case of the version with the tuple, it has a different type. So

 min (a, b) = if a > b then b else a
 min :: Ord p => (p, p) -> p

And the types (a, a) -> a and a -> a -> a are incompatible. You cannot curry the function with the tuple because it only has one parameter anyway and does not return a function type.

Upvotes: 4

chi
chi

Reputation: 116139

In the libraries, we have a function curry which transforms

f :: (A,B) -> C
f (a,b) = ...

into g = curry f, defined as

g :: A -> B -> C
g a b = ...

We also have the inverse transformation, called uncurry which maps g back into f = uncurry g.

Sometimes, functions like g are called "curried" and functions like f are called "uncurried". This way of speaking is a bit informal since we should instead say, more properly, "g is the curried form of f" and "f is the uncurried form of g".

There is no perfect distinction between "curried" and "uncurried" functions on ther own. For instance, h :: (A,B) -> C -> D can be seen as the currying of h1 :: ((A,B),C) -> D or the uncurrying of h2 :: A -> B -> C -> D, so it would be both curried and uncurred at the same time.

Often, though, we say that a function is curried or not depending on whether it takes a tuple as a single argument or its components as separate arguments. Again, this is a bit informal.

When choosing which style to follow, in Haskell we prefer the curried form since it is easier to apply partially.

Upvotes: 2

Related Questions