
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.

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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Cale Gibbard wrote:
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.
Alright, that was my guess (though I had no idea which was which).
I expect you'll replace ints with a more interesting list, as ints !! i is currently equal to i.
Yeah. I'm actually writing the PRNG component of RC4. I figured that would be a simple yet interesting problem to get started with Haskell. But I wanted to show the most minimal function I could that still illustrates the problem I have.
If you need to coerce something of integral (Int, Integer) type to any numeric type, use fromIntegral.
Okay, I just guessed that I should use it like this: j = ints !! fromIntegral( i ) That seems to work, so I'll go with it. Thanks! Cheres, Daniel.
participants (2)
-
Cale Gibbard
-
Daniel Carrera