Haskell error on number operations -


i've been searching god knows how long, couldn't piece of code work:

solution n = 3/2*((n-n`mod`3)+3) 

it compiles when call it, throws 2 errors, first 1 being

no instance (integral a0) arising use of `solution' 

here i'm trying achieve

solution 9 = 3/2* ((n-n`mod`3)+3) = 3/2 * ((9-0)+3) = 3/2 * (12) = 18 

haskell won't let add fractional (3/2) integral

((n - n `mod` 3) + 3) 

without explicitly telling want adding fromintegral latter.

solution n = 3/2 * fromintegral ((n-n`mod`3)+3) 

should work.

there's quite overview of numeric types in real world haskell. never remember details though, , add in fromintegral whenever it's required.


Comments