Couldn't match expected type `Integer' against inferred type `Int'

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.

On 9 August 2011 10:49, Paul Reiners
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
digitToInt returns an Int; as such the result of asInt_foldAux is an Int, but you've specified in its type signature that it should be returning an Integer.
Note that I'm using Int, rather than Integer, to avoid silent overflow errors.
Should that be the other way round? -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

On Aug 8, 2011, at 7:59 PM, Ivan Lazar Miljenovic wrote:
On 9 August 2011 10:49, Paul Reiners
wrote: 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
digitToInt returns an Int; as such the result of asInt_foldAux is an Int, but you've specified in its type signature that it should be returning an Integer.
So is there something like digitToInteger? Or can I somehow cast the Int to an Integer?
Note that I'm using Int, rather than Integer, to avoid silent overflow errors.
Should that be the other way round?
Yes, it should be the other way round.

On 9 August 2011 11:06, Paul Reiners
On Aug 8, 2011, at 7:59 PM, Ivan Lazar Miljenovic wrote:
On 9 August 2011 10:49, Paul Reiners
wrote: step acc x = acc * 10 + digitToInt x
digitToInt returns an Int; as such the result of asInt_foldAux is an Int, but you've specified in its type signature that it should be returning an Integer.
So is there something like digitToInteger? Or can I somehow cast the Int to an Integer?
step acc x = acc * 10 + fromIntegral (digitToInt x) -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com
participants (2)
-
Ivan Lazar Miljenovic
-
Paul Reiners