[Haskell-cafe] Couldn't match expected type `Integer' against inferred type `Int'

Paul Reiners paul.reiners at gmail.com
Tue Aug 9 02:49:50 CEST 2011


Why am I getting this error:

>     Couldn't match expected type `Integer' against inferred type `Int'
>     In the expression: foldl step 0 xs
>     In the definition of `asInt_foldAux':
>         asInt_foldAux xs
>                         = foldl step 0 xs
>                         where
>                             step acc '.' = error "cannot handle decimal numbers"
>                             step acc x = acc * 10 + digitToInt x

for this code?

> import Data.Char (digitToInt)
> 
> asInt_fold :: String -> Integer
> asInt_fold ('-':xs) = -(asInt_foldAux xs)
> asInt_fold xs       = asInt_foldAux xs
> 
> asInt_foldAux :: String -> Integer
> asInt_foldAux xs = foldl step 0 xs
>            where step acc '.' = error "cannot handle decimal numbers"
>                  step acc x = acc * 10 + digitToInt x

Note that I'm using Int, rather than Integer, to avoid silent overflow errors.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110808/77a1bf73/attachment.htm>


More information about the Haskell-Cafe mailing list