How do you define and use curry and uncurry (Prelude functions) in Haskell?

How do I use functions curry

and uncurry

preludes in Haskell?

Also, why would the following definitions cause a download error?

curry' :: ((a -> b) -> c) -> a -> b -> c
curry' f = \x y -> f (x, y)

uncurry' :: a -> b -> c -> ((a -> b) -> c)
uncurry' f = \(x,y) -> f x y

      

+3


source to share


1 answer


You get errors because your type signatures are wrong, you should use tuples instead of functions for arguments, a

and b

:

curry' :: ((a, b) -> c) -> a -> b -> c
uncurry' :: (a -> b -> c) -> ((a, b) -> c)

      

Also note the parentheses I added to the type uncurry'

, which are important in this case. What you have is equivalent to

uncurry' :: a -> (b -> (c -> ((a -> b) -> c)))

      

It is not the same, it is a function that takes 3 arguments and creates a function instead of a function that takes a function of 2 arguments and returns a function of one tuple argument.

You can use these functions like

> uncurry (+) (1, 2)
3
> curry fst 1 2
1
> curry snd 1 2
2

      



(I have not seen any other functions Prelude

that take tuples as arguments)

EDIT: On requesting chi, here's a clearer explanation of that last sentence:

a -> (b -> (c -> ((a, b) -> c)))

      

- a type of function that takes 3 arguments a

, b

and c

, and returns a function of type (a, b) -> c

.

(a -> b -> c) -> ((a, b) -> c)

      

- the type of a function that takes one argument a -> b -> c

and returns a function (a, b) -> c

.

+9


source







All Articles