[Haskell-beginners] Enum for natural numbers

Chaddaï Fouché chaddai.fouche at gmail.com
Mon Dec 21 05:48:02 EST 2009

On Mon, Dec 21, 2009 at 11:28 AM, Daniel Fischer
<daniel.is.fischer at web.de> wrote:
> Am Montag 21 Dezember 2009 10:56:06 schrieb kane96 at gmx.de:
>> Now everything works but to print Z also for negative Integers. Don't know
>> how to implement the <=0 or le0 in this case again:
>> instance Enum Nat where
>>         toEnum 0 = Z
>>         toEnum (n+1) = S(toEnum n)
> Note that many frown upon (n+k)-patterns and they may be removed from the language in
> future.

In fact they _have_been_ removed from the standard in Haskell 2010, so
in ghc 6.14 they'll become an extension instead of working by default.
You just have to use n as a pattern and (n-1) instead of n in the body.


More information about the Beginners mailing list