
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.