Home > OS >  Show result in Haskell
Show result in Haskell

Time:06-10

I'm very new to haskell.

How can I return (x1,x2) and print it out from my code?

qqq x
   | x < 0  x1 = mod (-x) 10 
   | 1 < x && x < 99 x1 = mod x 10
   | x2 = mod x 10

CodePudding user response:

You are using guards the wrong way. You seem to see these as if statements, that you then can use for assignements. In Haskell, you do not assign values to a variable, you declare these. You can work with:

qqq :: Integral a => a -> (a, a)
qqq x
   | x < 0 = (mod (-x) 10, x2)
   | 1 < x && x < 99 = (mod x 10, x2)
  where x2 = mod x 10

Here each guard thus has a condition before the equation sign (=), and at the right side returns a 2-tuple with as first item an expression for x1, and as second item x2.

You should also implement extra case(s) for x == 1 and x >= 99, these are not covered by the two guards.

  • Related