Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Show result in Haskell

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

Thanks for any help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading