
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

On Dec 17, 2009, at 12:21 , kane96@gmx.de wrote:
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
Take a look at guards. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

ok, but I shall use Enum in the exercise. -------- Original-Nachricht --------
Datum: Thu, 17 Dec 2009 13:56:30 -0500 Von: "Brandon S. Allbery KF8NH"
An: kane96@gmx.de CC: "Brandon S. Allbery KF8NH" , beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers
On Dec 17, 2009, at 12:21 , kane96@gmx.de wrote:
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
Take a look at guards.
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
-- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

2009/12/17
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Fill in the gaps: instance Enum Nat where toEnum x | x <= 0 = ... | otherwise = ... -- Deniz Dogan

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? -------- Original-Nachricht --------
Datum: Fri, 18 Dec 2009 18:06:32 +0100 Von: Deniz Dogan
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers
2009/12/17
: 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@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

2009/12/18
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
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers 2009/12/17
: 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@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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Deniz Dogan

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
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers
2009/12/18
: 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
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers 2009/12/17
: 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@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@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

2009/12/20
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?
In your current definition of toEnum, you always return "S Z" (in other words, "1", "the successor of zero") for any integer i > 0. This is clearly incorrect as you said. Your "otherwise" guard is correct, but the other one is not. I get the feeling that this exercise is about learning how to use recursion, so look into that. A few relevant links: http://en.wikibooks.org/wiki/Haskell/Recursion (the factorial example) http://learnyouahaskell.com/recursion http://book.realworldhaskell.org/read/functional-programming.html Hope that helps -- Deniz Dogan

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) fromEnum Z = 0 fromEnum (S n) = 1 + fromEnum n -------- Original-Nachricht --------
Datum: Sun, 20 Dec 2009 14:45:02 +0100 Von: Deniz Dogan
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers
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
2009/12/20
: 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?
In your current definition of toEnum, you always return "S Z" (in other words, "1", "the successor of zero") for any integer i > 0. This is clearly incorrect as you said. Your "otherwise" guard is correct, but the other one is not. I get the feeling that this exercise is about learning how to use recursion, so look into that.
A few relevant links: http://en.wikibooks.org/wiki/Haskell/Recursion (the factorial example) http://learnyouahaskell.com/recursion http://book.realworldhaskell.org/read/functional-programming.html
Hope that helps
-- Deniz Dogan
-- Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 - sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser

2009/12/21
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) fromEnum Z = 0 fromEnum (S n) = 1 + fromEnum n
Again, look into guards. You are definitely on the right track. Again, you should look into guards. instance Enum Nat where toEnum n | n <= 0 = Z | otherwise = ... -- do what with n? fromEnum Z = 0 fromEnum (S n) = 1 + fromEnum n You are on the right track. -- Deniz Dogan

-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 11:25:46 +0100 Von: Deniz Dogan
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers
2009/12/21
: 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) fromEnum Z = 0 fromEnum (S n) = 1 + fromEnum n
Again, look into guards. You are definitely on the right track.
Again, you should look into guards.
instance Enum Nat where toEnum n | n <= 0 = Z | otherwise = ... -- do what with n? fromEnum Z = 0 fromEnum (S n) = 1 + fromEnum n
I did it that way: instance Enum Nat where toEnum n | n <= 0 = Z | otherwise = S(toEnum (n-1)) fromEnum Z = 0 fromEnum (S n) = 1 + fromEnum n In another task I should create an infinite list: allNats :: [Nat] I would have done it in a recursion but there issn't a parameter to call allNats with -- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 15:06:38 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] Enum for natural numbers
Am Montag 21 Dezember 2009 15:02:16 schrieb kane96@gmx.de:
In another task I should create an infinite list: allNats :: [Nat] I would have done it in a recursion but there issn't a parameter to call allNats with
Prelude> :t iterate iterate :: (a -> a) -> a -> [a]
this works: allNats = (iterate (1+) 1) but it doesn't match my declaration: allNats :: [Nat] -- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

your type is data Nat = S Nat | Z and not data Nat = 1 + Nat | 0 On Mon, 2009-12-21 at 18:27 +0100, kane96@gmx.de wrote:
-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 15:06:38 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] Enum for natural numbers Am Montag 21 Dezember 2009 15:02:16 schrieb kane96@gmx.de:
In another task I should create an infinite list: allNats :: [Nat] I would have done it in a recursion but there issn't a parameter to call allNats with
Prelude> :t iterate iterate :: (a -> a) -> a -> [a]
this works: allNats = (iterate (1+) 1) but it doesn't match my declaration: allNats :: [Nat]

-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 19:40:23 +0100 Von: jean verdier
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers
your type is data Nat = S Nat | Z and not data Nat = 1 + Nat | 0
Do I have to use something like fromEnum (S Z)? Or or the iteration wrong?
On Mon, 2009-12-21 at 18:27 +0100, kane96@gmx.de wrote:
-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 15:06:38 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] Enum for natural numbers Am Montag 21 Dezember 2009 15:02:16 schrieb kane96@gmx.de:
In another task I should create an infinite list: allNats :: [Nat] I would have done it in a recursion but there issn't a parameter to call allNats with
Prelude> :t iterate iterate :: (a -> a) -> a -> [a]
this works: allNats = (iterate (1+) 1) but it doesn't match my declaration: allNats :: [Nat]
-- Preisknaller: GMX DSL Flatrate für nur 16,99 Euro/mtl.! http://portal.gmx.net/de/go/dsl02

-------- Original-Nachricht --------
Datum: Mon, 21 Dec 2009 22:06:46 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de, jean verdier Betreff: Re: [Haskell-beginners] Enum for natural numbers
Am Montag 21 Dezember 2009 21:02:06 schrieb kane96@gmx.de:
your type is data Nat = S Nat | Z and not data Nat = 1 + Nat | 0
Do I have to use something like fromEnum (S Z)? Or or the iteration wrong?
Which operation on Nat corresponds to (1 +) on Int[eger] ?
I implemented it: allNats :: [Nat] allNats = iterate S Z -- Preisknaller: GMX DSL Flatrate für nur 16,99 Euro/mtl.! http://portal.gmx.net/de/go/dsl02

Am Montag 21 Dezember 2009 10:56:06 schrieb kane96@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. Use guards: toEnum n | condition1 = rhs1 | condition2 = rhs2
fromEnum Z = 0 fromEnum (S n) = 1 + fromEnum n

On Mon, Dec 21, 2009 at 11:28 AM, Daniel Fischer
Am Montag 21 Dezember 2009 10:56:06 schrieb kane96@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. -- Jedaï

Am Sonntag 20 Dezember 2009 14:36:06 schrieb kane96@gmx.de:
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?
Your task is to write an Enum instance for Nat. class Enum a where succ :: a -> a pred :: a -> a toEnum :: Int -> a fromEnum :: a -> Int enumFrom :: a -> [a] enumFromThen :: a -> a -> [a] enumFromTo :: a -> a -> [a] enumFromThenTo :: a -> a -> a -> [a] So you have to write functions succ :: Nat -> Nat -- that's very easy pred :: Nat -> Nat -- easy too, except you have to decide whether to throw an error on pred Z or have pred Z = Z (which would fit with the required toEnum) toEnum :: Int -> Nat -- that has the additional requirement that it should return Z for negative input and so on. The first thing is to understand the Nat datatype. That models the so-called Peano numbers (or, put another way, it's built to mirror the Peano axioms for the natural numbers). Z corresponds to 0 S Z corresponds to 1, which is the successor of 0 S (S Z) corresponds to 2, which is the successor of 1 ... For non-negative n, toEnum n should be the Peano number corresponding to n and for negative n, toEnum n should be Z (to avoid exceptions). So, toEnum n | n < 0 = Z toEnum 0 = Z toEnum n = ? -- here, we know n > 0 fromEnum should be the correspondnece the other way round, so fromEnum Z = 0 fromEnum (S p) = ? -- which Int corresponds to the successor of p?

-------- Original-Nachricht --------
Datum: Sun, 20 Dec 2009 14:59:45 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] Enum for natural numbers
Am Sonntag 20 Dezember 2009 14:36:06 schrieb kane96@gmx.de:
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?
Your task is to write an Enum instance for Nat.
class Enum a where succ :: a -> a pred :: a -> a toEnum :: Int -> a fromEnum :: a -> Int enumFrom :: a -> [a] enumFromThen :: a -> a -> [a] enumFromTo :: a -> a -> [a] enumFromThenTo :: a -> a -> a -> [a]
So you have to write functions
succ :: Nat -> Nat -- that's very easy pred :: Nat -> Nat -- easy too, except you have to decide whether to throw an error on pred Z or have pred Z = Z (which would fit with the required toEnum)
toEnum :: Int -> Nat -- that has the additional requirement that it should return Z for negative input
and so on.
The first thing is to understand the Nat datatype. That models the so-called Peano numbers (or, put another way, it's built to mirror the Peano axioms for the natural numbers). Z corresponds to 0 S Z corresponds to 1, which is the successor of 0 S (S Z) corresponds to 2, which is the successor of 1 ...
For non-negative n, toEnum n should be the Peano number corresponding to n and for negative n, toEnum n should be Z (to avoid exceptions). So,
toEnum n | n < 0 = Z toEnum 0 = Z toEnum n = ? -- here, we know n > 0
fromEnum should be the correspondnece the other way round, so
fromEnum Z = 0 fromEnum (S p) = ? -- which Int corresponds to the successor of p?
what is P? Now I read some short textes about it and think I know more or less what I have to do for the exercise. But I don't know really how. Do you know any examples for it, how it normally looks like? -- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Am Sonntag 20 Dezember 2009 21:06:36 schrieb kane96@gmx.de:
So,
toEnum n | n < 0 = Z toEnum 0 = Z toEnum n = ? -- here, we know n > 0
fromEnum should be the correspondnece the other way round, so
fromEnum Z = 0 fromEnum (S p) = ? -- which Int corresponds to the successor of p?
what is P?
Any element of Nat (p for Peano).
Now I read some short textes about it and think I know more or less what I have to do for the exercise. But I don't know really how. Do you know any examples for it, how it normally looks like?
Deniz Dogan posted a few links to recursion earlier today, if you look at them, you should get the general idea. As a further example, replicate :: Int -> a -> [a] replicate n x | n <= 0 = [] | otherwise = x:replicate (n-1) x may help.

-------- Original-Nachricht --------
Datum: Sun, 20 Dec 2009 21:28:21 +0100 Von: Daniel Fischer
An: beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers
Am Sonntag 20 Dezember 2009 21:06:36 schrieb kane96@gmx.de:
So,
toEnum n | n < 0 = Z toEnum 0 = Z toEnum n = ? -- here, we know n > 0
fromEnum should be the correspondnece the other way round, so
fromEnum Z = 0 fromEnum (S p) = ? -- which Int corresponds to the successor of p?
what is P?
Any element of Nat (p for Peano).
Now I read some short textes about it and think I know more or less what
I
have to do for the exercise. But I don't know really how. Do you know any examples for it, how it normally looks like?
Deniz Dogan posted a few links to recursion earlier today, if you look at them, you should get the general idea. As a further example,
replicate :: Int -> a -> [a] replicate n x | n <= 0 = [] | otherwise = x:replicate (n-1) x
may help.
my problem is that I don't know how to use Enum correctly and didn't find any helpfull example
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 - sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser

2009/12/20
-------- Original-Nachricht --------
Datum: Sun, 20 Dec 2009 21:28:21 +0100 Von: Daniel Fischer
An: beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers Am Sonntag 20 Dezember 2009 21:06:36 schrieb kane96@gmx.de:
So,
toEnum n | n < 0 = Z toEnum 0 = Z toEnum n = ? -- here, we know n > 0
fromEnum should be the correspondnece the other way round, so
fromEnum Z = 0 fromEnum (S p) = ? -- which Int corresponds to the successor of p?
what is P?
Any element of Nat (p for Peano).
Now I read some short textes about it and think I know more or less what
I
have to do for the exercise. But I don't know really how. Do you know any examples for it, how it normally looks like?
Deniz Dogan posted a few links to recursion earlier today, if you look at them, you should get the general idea. As a further example,
replicate :: Int -> a -> [a] replicate n x | n <= 0 = [] | otherwise = x:replicate (n-1) x
may help.
my problem is that I don't know how to use Enum correctly and didn't find any helpfull example
If I may say so, I don't think that your understanding of Enum is the problem here, it's the lack of understanding of Haskell in general, but specifically algebraic data types[1] and recursion[2]. [1] data Maybe a = Just a | Nothing [2] f n = f (n - 1) + f (n - 2) -- Deniz Dogan

kane96@gmx.de wrote:
-------- Original-Nachricht --------
Datum: Sun, 20 Dec 2009 21:28:21 +0100 Von: Daniel Fischer
An: beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers Am Sonntag 20 Dezember 2009 21:06:36 schrieb kane96@gmx.de:
So,
toEnum n | n < 0 = Z toEnum 0 = Z toEnum n = ? -- here, we know n > 0
fromEnum should be the correspondnece the other way round, so
fromEnum Z = 0 fromEnum (S p) = ? -- which Int corresponds to the successor of p? what is P? Any element of Nat (p for Peano).
Now I read some short textes about it and think I know more or less what I have to do for the exercise. But I don't know really how. Do you know any examples for it, how it normally looks like? Deniz Dogan posted a few links to recursion earlier today, if you look at them, you should get the general idea. As a further example,
replicate :: Int -> a -> [a] replicate n x | n <= 0 = [] | otherwise = x:replicate (n-1) x
may help.
my problem is that I don't know how to use Enum correctly and didn't find any helpfull example
Enum is used for things that are in sequence and can be numbered likewise. For example, (if you consider Monday the first day of the week) data Weekday = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday probably Monday would correspond to 0, Tuesday to 1, Wednesday to 2, and so on through Sunday corresponding to 6. In your problem, data Nat = Z | S Nat Z corresponds to 0, (S Z) corresponds to 1, (S (S Z)) corresponds to 2, and so on forever. Perhaps you can see this by seeing "Z" as "0" and "S" as "1 +". Z <-> 0 (S Z) <-> (1 + 0) (S (S Z)) <-> (1 + (1 + 0)) etc... but you'll have to find a way to define this pattern with just a small number of cases, not a huge infinite number. -Isaac

Am Sonntag 20 Dezember 2009 21:35:45 schrieb kane96@gmx.de:
As a further example,
replicate :: Int -> a -> [a] replicate n x
| n <= 0 = [] | otherwise = x:replicate (n-1) x
may help.
my problem is that I don't know how to use Enum correctly and didn't find any helpfull example
toEnum and fromEnum should give the correspondence 0 <-> Z 1 <-> S Z 2 <-> S (S Z) 3 <-> S (S (S Z)) 4 <-> S (S (S (S Z))) 5 <-> S (S (S (S (S Z)))) and so on. Of course, defining toEnum entirely via pattern matching toEnum 0 = Z toEnum 1 = S Z toEnum 2 = S (S Z) toEnum 3 = S (S (S Z)) ... will take too long to type (and will drive the compiler mad), so define it via recursion. It will strongly resemble the definition of replicate above. The definition of fromEnum will resemble that of genericLength, http://www.haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/src/Data- List.html#genericLength
participants (7)
-
Brandon S. Allbery KF8NH
-
Chaddaï Fouché
-
Daniel Fischer
-
Deniz Dogan
-
Isaac Dupree
-
jean verdier
-
kane96@gmx.de