[Haskell-cafe] Int vs Integer?

Cale Gibbard cgibbard at gmail.com
Thu May 5 04:23:47 EDT 2005


Int is the type of machine integers, with guaranteed range at least
-2^29 to 2^29 - 1, while Integer is arbitrary precision integers, with
range as large as you have memory for.

The code you gave, on its own, is fine. The types inferred by ghci are
ints :: [Int]
and
prng :: [[Int]]

Hugs seems to choke on it though. If you provide an explicit type
signature for ints, it works fine. It seems to be defaulting the type
to [Integer] a little too early. I expect you'll replace ints with a
more interesting list, as ints !! i is currently equal to i.

If you need to coerce something of integral (Int, Integer) type to any
numeric type, use fromIntegral.

hope this helps,
 - Cale
On 5/5/05, Daniel Carrera <dcarrera at digitaldistribution.com> wrote:
> Hello,
> 
> What's the difference between "Int" and "Integer"?
> 
> How can I tell Haskell which one I want? I'm having trouble with a function:
> 
> ints = 0 : map (1+) ints
> prng = map iter ints
>       where iter 0 = [0,0]
>             iter n = [i,j]
>                  where i = mod n 256
>                        j = ints !! i
> 
> (eventually 'iter' will be more a more interesting function).
> 
> When I try to load this module in Hugs I get the error:
> 
> ERROR "./PRNG.hs":24 - Type error in list
> *** Expression     : [i,j]
> *** Term           : j
> *** Type           : Integer
> *** Does not match : Int
> 
> Line 24 is the 'prng' line. I've spent all day trying to fix this, but
> for the life of me I can't figure out why it happens or how to make it
> stop. Haskell just doesn't seem to like the 'iter !! i' construct.
> 
> It's interesting that if you replace 'i' by (say) 3, Haskell runs the
> program happily. But, if you replace 'i = mod n 256' by 'i=3' you get
> the same error.
> 
> Any words of wisdom would be appreciated.
> 
> Cheers,
> Daniel.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list