[Haskell-cafe] Re: Positive integers

Ben Rudiak-Gould Benjamin.Rudiak-Gould at cl.cam.ac.uk
Thu Mar 23 23:16:00 EST 2006


Daniel McAllansmith wrote:
> I can see the domain bounds check would be a problem in theory, but in 
> practice doesn't the type enforce that?  Keeping Word positive costs nothing 
> because it just overflows.  Wouldn't it be much the same?

If you're planning wraparound semantics then you're better off with Int 
indexes. Passing an index congruent to -1 mod 2^32 is certainly an error, 
and (!!) may as well fail immediately rather than try to traverse 2^32-1 
list nodes. I think both Int and Word are equally "correct" here, since both 
are proper supersets of the valid set of indexes. (If you're working with 
lists so long that Int may not be big enough, you shouldn't trust Word either.)

Haskell 98's List module supplies "generic" versions of the list functions, 
like genericIndex :: Integral a => [b] -> a -> b, which will work with Word.

-- Ben



More information about the Haskell-Cafe mailing list