[Haskell-beginners] curry in a hurry
prad
prad at towardsfreedom.com
Sat Jul 3 18:19:24 EDT 2010
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?
--
In friendship,
prad
... with you on your journey
Towards Freedom
http://www.towardsfreedom.com (website)
Information, Inspiration, Imagination - truly a site for soaring I's
More information about the Beginners
mailing list