Here is my code:
n = [(a,b) | a <- [1..5],b <- [1..5]]
calcBmis xs = [bmi | (w, h) <- xs,let bmi = w / h ^ 2]
When trying to apply calcBmis to n, I get the following error:
*Charana> calcBmis n
<interactive>:220:1:
No instance for (Fractional Integer)
arising from a use of ‘calcBmis’
In the expression: calcBmis n
In an equation for ‘it’: it = calcBmis n
Further investigation in ghci:
*Charana> :t calcBmis
calcBmis :: Fractional t => [(t, t)] -> [t]
*Charana> :t n
n :: [(Integer, Integer)]
What I'm assuming is that the list I produce is of type (Integer,Integer), but that cannot be processed in the calcBmis, which only takes in Fractional. Any idea how to fix this problem?
you can use
divinstead of(/):as you can see this version can deal with all
Integralvalues - but of course will truncate (because ofdiv).or you can map everything with
fromIntegral:Which then will produce fractional values:
in either case it will work with all inputs as long as they are an instance of
Integral- the second version will even accept pairs of different integrals ;)