Type casting in haskell, Fractional and Int -
i've written function (should) takes infinite list of booleans , calculate ratio of true false values on first n elements:
prob n list = foldr (+) 0 (map booltoint (take n list)) / n     booltoint b         | b == true = 1         | otherwise = 0   unfortunately that's not working:
no instance (fractional int)   arising use of `/' possible fix: add instance declaration (fractional int) in expression: foldr (+) 0 (map booltoint (take n list)) / n in equation `prob':     prob n list       = foldr (+) 0 (map booltoint (take n list)) / n                 booltoint b             | b == true = 1             | otherwise = 0 failed, modules loaded: none.   i tried make conversion, isn't working either:
prob n list = foldr (+) 0 (map booltoint (take (fromintegral (tointeger n)) list)) / n     booltoint b         | b == true = 1         | otherwise = 0   it's compiling, try call function error:
*main> prob 50 tocoin1 <interactive>:1:6: ambiguous type variable `a0' in constraints:   (num a0) arising literal `50' @ <interactive>:1:6-7   (integral a0) arising use of `prob' @ <interactive>:1:1-4   (fractional a0) arising use of `prob' @ <interactive>:1:1-4 probable fix: add type signature fixes these type variable(s) in first argument of `prob', namely `50' in expression: prob 50 tocoin1 in equation `it': = prob 50 tocoin1   any suggestions?
you're converting @ wrong place. try sticking fromrational around entire foldr , n.
prob n list = fromintegral count / fromintegral n     count = foldr (+) 0 (map booltoint (take n list))           booltoint b             | b == true = 1             | otherwise = 0   oh, , booltoint function identical fromenum specialized bools.
prob n list = fromintegral count / fromintegral n     count = foldr (+) 0 (map fromenum (take n list))   the fundamental problem tried imposed conflicting requirements on first argument prob. use of tointeger constrained n integral, use in / required fractional, , there's no type that's both integral , fractional.
Comments
Post a Comment