exercise 4.19

Ketil Malde [email protected]
09 Oct 2001 13:51:08 +0200


"riz er" <[email protected]> writes:

> howMany :: Int -> Int -> Int -> Int
> howMany n1 n2 n3
> 	| (n1 > a) && (n2 > a) && (n3 > a) = 3
> 	| (n1 > a) && (n2 > a) = 2
> 	| (n1 > a) && (n3 > a) = 2
> 	| otherwise = 1
> 	  where a = (n1 + n2 + n3)/3

> i get an error message
> "Instance of Fractional Int required for definition of howMany"

The problem is that since Haskell knows that n1 et al is an Int, it
deduces that a must be an Int (since (>) compares values of the same
type).  Unfortunately, you're dividing with (/), which provides
fractional answers, and not necessarily integers, and certainly not
Ints. 

The easy way out is to consider a different division operator, but you
may want to consider the behaviour of your program in the obvious
borderline case.

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants