
Hi, I am doing an exercise in Haskell, which is converting a string like “$123.312” to double value. Below is my code: module Main where import Data.Char caluInt l = foldl1 (\acc x -> acc * 10 + x) (map digitToInt l) caluDecimal l = (foldr1 (\x acc -> acc / 10.0 + x) (map digitToInt l)) convert(x:xs) = let num = [e | e <- xs, e /= ','] intPart = takeWhile (/='.') num decimalPart = tail(dropWhile (/='.') num) in (caluInt intPart) + (caluDecimal decimalPart) And I got an error in this line: caluDecimal l = (foldr1 (\x acc -> acc / 10.0 + x) (map digitToInt l)), which says: No instance for (Fractional Int) arising from a use of `/' Possible fix: add an instance declaration for (Fractional Int) In the first argument of `(+)', namely `acc / 10.0' In the expression: acc / 10.0 + x In the first argument of `foldr1', namely `(\ x acc -> acc / 10.0 + x)' Why Haskell insists that 10.0 is a Int? How can I explicitly tell Haskell I want a Fractional? -- Cui Liqiang

On Sun, Oct 26, 2014 at 6:52 PM, Cui Liqiang
Why Haskell insists that 10.0 is a Int? How can I explicitly tell Haskell I want a Fractional?
Because digitToInt means exactly what it says. If you want it to become something other than Int, apply fromIntegral to its result. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
participants (2)
-
Brandon Allbery
-
Cui Liqiang