[Haskell-beginners] Enum for natural numbers

kane96 at gmx.de kane96 at gmx.de
Sun Dec 20 08:36:06 EST 2009


Maybe I didn't understand the exercise if have to do. It says:
"Write the instance Enum Nat where toEnum is defined as a total function that returns Z for negative integers. Some examples:
*Main> toEnum (-1) :: Nat
Z
*Main> toEnum 0 :: Nat
Z
*Main> toEnum 1 :: Nat
S Z
*Main> fromEnum (S (S Z))
2

so I did:
data Nat = Z | S Nat deriving (Eq,Ord,Show)
instance Enum Nat where
toEnum x|x > 0 = S Z
        |otherwise = Z


somehow it looks really wrong. Do you understand want I have to do and how it should look like?


-------- Original-Nachricht --------
> Datum: Fri, 18 Dec 2009 18:59:40 +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/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

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01


More information about the Beginners mailing list