newbie question regarding type declarations
Greg Buchholz
sleepingsquirrel at sleepingsquirrel.org
Fri Oct 31 22:34:35 EST 2003
On Fri, 31 Oct 2003, gangadhar npk wrote:
> hi,
> 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
> errors
>
> 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...
square(3, 4)
square(3.14, 4)
square(2.718, 6.022)
etc.
Greg Buchholz
More information about the Haskell-Cafe
mailing list