Reputation: 16224
Curried
add::Int->Int->Int
add x y = x+y
Uncurried
add1::(Int,Int)->Int
add1 (x,y)=x+y
I have several problems regrading Curried and Uncurried function , consider the functions above,
in Uncurried function input parameters are as (Int,Int)
, so is it equalent to a input a tuple ? how can we differentiated that ?
What are the pros and cons regarding uncurried and curried functions? When and why should which be used?
Upvotes: 2
Views: 4280
Reputation: 28097
I like @bzn's answer, but I wanted to give a few examples of where uncurried functions are useful.
Some libraries make heavy use of tuples of data. One example is Gtk2hs, which uses a tuple (Int, Int)
for window sizes and certain coordinates. So when I'm working with gtk2hs, I'll often write functions in an uncurried form so I don't have to manually unpack the tuple.
Also remember that a function can only return one result. To return more than one value, all the results need to be packed into a tuple. uncurry
is then useful to make compositions out of those functions. Here's a simplified example from a project I'm working on:
addIndex :: MyData -> (Int, MyData)
normalize' :: Int -> MyData -> [(Int, MyData)]
normalize :: [MyData] -> [(Int, MyData)]
normalize = concatMap (uncurry normalize' . addIndex)
I usually prefer writing functions in a curried form, but here I needed the uncurried version of normalize'
to compose with addIndex
.
These are the two situations where I find an uncurried function useful. Fortunately it's easy to convert between the two forms.
Upvotes: 4
Reputation: 2392
in Uncurried function input parameters are as (Int,Int) , so is it equalent to a input a tuple ? how can we differentiated that ?
It's not only equivalent, but it is a tuple. So add1
is a function that takes a pair (2-tuple) of Ints and returns an Int.
Whats the pros and congs regarding uncurried and curried functions ? , and where to use them ?
As a rule of thumb, I'd say: Always use curried functions, if you don't have a good reason not to.
They have the nice feature that you can partially apply them, e.g. you can write f = add 1
, where f
now has the type f :: Int -> Int
and always adds 1 to its argument.
This has many applications and is very common in Haskell, because it's so convenient. E.g. a function that adds 1 to all elements in a list would simply be map (add 1)
.
Additionally the syntax is much less noisy.
Upvotes: 9