[Haskell-beginners] Enum for natural numbers

Deniz Dogan deniz.a.m.dogan at gmail.com
Fri Dec 18 12:59:40 EST 2009


2009/12/18  <kane96 at gmx.de>:
> ahhh, ok, thanks so far. Here is how the output should looks like:
> *Main> toEnum (-1) :: Nat
> Z
> *Main> toEnum 0 :: Nat
> Z
> *Main> toEnum 1 :: Nat
> S Z
> *Main> fromEnum (S (S Z))
> 2
>
> the first three things work, but I don't know about the last one. Does anybody how it should work and what's the sense of it?
>

Looks right to me. "Z" means 0 (Zero), "S x" means "the Successor of
x". So (S (S (S Z))) means "the successor of the successor of the
successor of zero", which is 3.

>
> -------- Original-Nachricht --------
>> Datum: Fri, 18 Dec 2009 18:06:32 +0100
>> Von: Deniz Dogan <deniz.a.m.dogan at gmail.com>
>> An: kane96 at gmx.de
>> CC: beginners at haskell.org
>> Betreff: Re: [Haskell-beginners] Enum for natural numbers
>
>> 2009/12/17  <kane96 at gmx.de>:
>> > Hi,
>> > I have data Nat = Z | S Nat deriving (Eq,Ord,Show)
>> >
>> > toEnum should return Z for negative numbers. I did something like this
>> but the problem is I don't know how to set "less than". I tried > and lt:
>> >
>> > instance Enum Nat where
>> >    toEnum (gt 0) = S Z
>> >    toEnum (lt 1) = Z
>> > --
>> > GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
>> > Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
>> > _______________________________________________
>> > Beginners mailing list
>> > Beginners at haskell.org
>> > http://www.haskell.org/mailman/listinfo/beginners
>> >
>>
>> Fill in the gaps:
>>
>> instance Enum Nat where
>>     toEnum x | x <= 0 = ...
>>              | otherwise = ...
>>
>> --
>> Deniz Dogan
>
> --
> Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
> sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
Deniz Dogan


More information about the Beginners mailing list