newbie question regarding type declarations
sleepingsquirrel at sleepingsquirrel.org
Fri Oct 31 22:34:35 EST 2003
On Fri, 31 Oct 2003, gangadhar npk wrote:
> I am a newbie to Haskell. I am reading the tutorial by Hal Daume III.
> Regarding the function types which are an extension of lambda calculus,
> I was wondering if at all it is possible to write a type that would
> return two values as the output - something like a square function
> which would take a pair and return a pair. I tried this, but there are
> square :: (Num a, Num b) => (a ,b)
> square (x , y) = (x*x , y*y)
> How can I specify that the square functions output would be a pair ?
I'm also a Haskell newb, but I believe "Num" is actually a class
of types, not an actual type itself. So you can't sustitute it for the
"Integer" type below...
square :: (Integer, Integer) -> (Integer, Integer)
...Instead you have to say...
square :: (Num a, Num b) => (a,b) -> (a,b)
...Note the use of both the big arrow (=>) and the small arrow (->). This
is the most general type you could make for this particular function and
allows you to mix amd match arguments like...
More information about the Haskell-Cafe