[Haskell-cafe] Creating a type for a subset of the integers

Brad Larsen brad.larsen at gmail.com
Tue Dec 18 21:07:25 EST 2007

Hi there list,

How would one go about creating a new type for a subset of the integers,  
for (contrived) example just the even integers?  I was thinking of making  
a new type

newtype EvenInt = EvenInt Integer

but the problem with this is that it accepts any integer, even odd ones.   
So to prevent this, the module does not export the constructor for  
it---rather, the module exports a function `mkEvenInt' that creates an  
EvenInt if the given value is acceptable or raises an error otherwise.

What's the right way to do this?  Thanks!

Brad Larsen

More information about the Haskell-Cafe mailing list