
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 ? thank you gangadhar

On Fri, Oct 31, 2003 at 05:30:06PM +0550, 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 ?
You forgot about function arguments in its type. It should be: square :: (Num a, Num b) => (a, b) -> (a, b) Next time try commenting out the type signature and see if the compiler can infer the type. Both Hugs and GHCi provide commands for inspecting types of values. Prelude> let square (x,y) = (x*x,y*y) Prelude> :type square forall a a1. (Num a1, Num a) => (a1, a) -> (a1, a) You can use :t as a shortcut.
thank you gangadhar
Best regards, Tom -- .signature: Too many levels of symbolic links

"gangadhar npk"
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 ?
Why don't you try it without the type declaration, and see what Hugs or GHCi thinks the type is? This should make it pretty clear what the problem is, I think. -kzm -- If I haven't seen further, it is by standing in the footprints of giants

On Fri, 31 Oct 2003 17:30:06 +0550
"gangadhar npk"
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 ?
You don't need to provide a type signature, Haskell can figure it out. You can use :type in a repl (e.g. Hugs, GHCi) to print out the type of an expression. So, you can either put 'square (x,y) = (x*x,y*y)' into a file and load it and do ':t square' or you can do something like, :t let square (x,y) = (x*x,y*y) in square and get ... <fill in the blank> Also, for future reference, if you need help with something it helps to provide all the information you can. Things like the actual text of the error messages, the implementation you are using, and the operating system you are using. Basically, the more information you can provide the better.

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
participants (5)
-
Derek Elkins
-
gangadhar npk
-
Greg Buchholz
-
ketil+haskell@ii.uib.no
-
Tomasz Zielonka