
I'm sure a more thorough answer would come from a more experienced Haskell user, but I'm gonna go and through my two cents just in case it helps: part 1: 'f' takes one argument (albeit one which is not concrete), 'g' takes two arguments. The Integer/Num discrepancy you experience is due to ghci pushing the types a bit... put your code in a file for better results. part 2: 'f x y' has this type: f :: (Num t) => t -> t -> t which you could read as... f :: (Num t) => t -> (t -> t) ... to make the curry'ing more evident. You'd see that 'f' takes a single argument, a 't' which belongs to type-class Num, and returns some unknown function with type 't -> t' (where the type constrain for 't' still holds). However, to keep it simple, just say that 'f' takes two arguments ;) part 3: I refuse to put myself through that twisted experiment of yours :P Seriously, though, I have no sufficient knowledge to answer with confidence. El sáb, 03-07-2010 a las 15:19 -0700, prad escribió:
today i'm trying to understand curry and uncurry (in 3 parts with specific questions labelled as subsections of the parts).
part 1
if we have
f (x,y) = x + y this is a function working on a single argument (x,y) albeit composed of 2 parameters. however, since all functions are curried in haskell (read that here: http://www.haskell.org/haskellwiki/Currying), what is really happening is
(f x) y or specific to this case (+ x) y since f ends up being (+) as defined.
now if we write g = curry f we are naming a function whose purpose is to (f x) whatever comes its way allowing us to do neat things like:
map (g 3) [4,5,6] [7,8,9]
which we can't do by map (f 3) [4,5,6] because f :: (Num t) => (t, t) -> t meaning f - is a function ... the (Num t) - applies itself ... the => - to a Pair of (Num t)s - gives back a Num t
1.1 am i reading the type statement correctly?
while g :: Integer -> Integer -> Integer means g takes an Integer, applies itself and that Integer to another Integer and computes another Integer.
1.2 how come these are Integer suddenly and not Num t?
this is a problem because while i can do f (2.3,9.3) i get an error when i try (g 2.3) 9.3 ghci wants an instance declaration for (Fractional Integer) which puzzles me because g came about through currying f which is fine with fractions.
part 2
now the next discovery is really strange to me.
if i name
f x y = x + y we see f :: (Num a) => a -> a -> a which looks like the curried form of f (x,y) in fact that's what it exactly is and i can do map (f 3) [4,5,6] [7,8,9] just as i did with g before!!
which is what the wiki statement says too:
f :: a -> b -> c is the curried form of g :: (a, b) -> c
however, it starts by stating:
"Currying is the process of transforming a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed." http://www.haskell.org/haskellwiki/Currying
2.1 so does all this mean that
f (x,y) is the function that takes multiple arguments and not a single argument as i initially thought
and
f x y is the function that actually takes a single argument twice?
part 3
some of the above seems to be confirmed by looking at these types
c x y = x + y c :: (Num a) => a -> a -> a so that's curried
u (x,y) = x + y u :: (Num t) => (t,t) -> t so that's uncurried
:t uncurry c uncurry c :: (Num a) => (a, a) -> a
:t curry u curry u :: (Num a) => a -> a -> a
but
:t uncurry u uncurry u :: (Num (b -> c)) => ((b -> c, b -> c), b) -> c we're trying to uncurry something that is already uncurried
and
:t curry c curry c :: (Num (a, b)) => a -> b -> (a, b) -> (a, b) we're trying to curry something that is already curried
3.1 just what are these strange looking things and how should their types be interpreted?