[Haskell-beginners] Haskell typing question

Cui Liqiang cui.liqiang at gmail.com
Sun Oct 26 22:52:08 UTC 2014


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141027/8e0bb3a0/attachment.html>


More information about the Beginners mailing list