How can I use curry
and uncurry
prelude functions in Haskell?
Moreover, why do the following definitions throw an error when loaded?
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
You're getting errors because your type signatures are wrong, you should be using tuples instead of functions for the a
and b
arguments:
curry' :: ((a, b) -> c) -> a -> b -> c
uncurry' :: (a -> b -> c) -> ((a, b) -> c)
Also, notice the parentheses I added to uncurry'
s type, those are important in this case. What you have is equivalent to
uncurry' :: a -> (b -> (c -> ((a -> b) -> c)))
Which is not the same, this is a function that takes 3 arguments and produces a function instead of a function that takes a 2 argument function and returns a function of one tuple argument.
You could use these functions like
> uncurry (+) (1, 2)
3
> curry fst 1 2
1
> curry snd 1 2
2
(I didn't see any other Prelude
functions that take tuples as arguments)
EDIT: At chi's request, here's a more visual explanation of that last sentence:
a -> (b -> (c -> ((a, b) -> c)))
is the type of a function that takes 3 arguments a
, b
, and c
, and returns a function of type (a, b) -> c
.
(a -> b -> c) -> ((a, b) -> c)
is the type of a function that takes a single argument a -> b -> c
and returns a function (a, b) -> c
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.