haskell - Confusing types in ghci -
here code:
n = [(a,b) | <- [1..5],b <- [1..5]] calcbmis xs = [bmi | (w, h) <- xs,let bmi = w / h ^ 2] when trying apply calcbmis n, following error:
*charana> calcbmis n <interactive>:220:1: no instance (fractional integer) arising use of ‘calcbmis’ in expression: calcbmis n in equation ‘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 list produce of type (integer,integer), cannot processed in calcbmis, takes in fractional. idea how fix problem?
you can use div instead of (/):
calcbmis xs = [ bmi | (w,h) <- xs, let bmi = (w `div` h)^2 ] prelude> :t calcbmis calcbmis :: integral t => [(t, t)] -> [t] prelude> calcbmis n [1,0,0,0,0,4,1,0,0,0,9,1,1,0,0,16,4,1,1,0,25,4,1,1,1] as can see version can deal integral values - of course truncate (because of div).
or can map fromintegral:
calcbmis xs = [ bmi | (w,h) <- xs, let bmi = (fromintegral w / fromintegral h)^2 ] prelude> :t calcbmis calcbmis:: (fractional t, integral t1, integral t2) => [(t1, t2)] -> [t] which produce fractional values:
prelude> calcbmis n [1.0,0.25,0.1111111111111111 ,6.25e-2 ,4.000000000000001e-2 ,4.0 ,1.0 ,0.4444444444444444 , ... ] in either case work inputs long instance of integral - second version accept pairs of different integrals ;)
Comments
Post a Comment