[Haskell-cafe] Positive integers
Malcolm.Wallace at cs.york.ac.uk
Fri Mar 24 06:29:08 EST 2006
Daniel McAllansmith <dagda at xtra.co.nz> wrote:
> Unless I've missed it, there is no typeclass for positive integers in
> GHC. Is there any particular reason it doesn't exist?
> Also, it seems Word would be a far better type in the likes of (!!),
> length, etc. Is it just tradition that resulted in the use of Int?
There is a good argument that what you really want here is the lazy
infinite natural numbers. Think Peano:
data Natural = Zero | Succ Natural
The clear correspondance between lazy lists and the size/index of a lazy
list makes this type fairly compelling. It can be jolly useful,
particularly with infinite streams, to be able to compute
(length xs > n)
without forcing the evaluation of the list to more than n places.
Of course, if the naturals were actually represented in Peano (unary)
form, that would be somewhat inefficient, but there are various
strategies that can be used to make the representation smaller whilst
retaining their nice properties.
More information about the Haskell-Cafe