Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Enum for natural numbers (Daniel Fischer)
2. Re: Pure Regex.PCRE Possible? (PLEAC, Fix Style) (Volkan YAZICI)
3. Re: Re: Pure Regex.PCRE Possible? (PLEAC, Fix Style)
(Daniel Fischer)
4. Re: Re: Pure Regex.PCRE Possible? (PLEAC, Fix Style)
(Daniel Fischer)
5. Re: Enum for natural numbers ([email protected])
6. Re: Enum for natural numbers (Daniel Fischer)
7. Re: Enum for natural numbers ([email protected])
8. Re: Enum for natural numbers (Deniz Dogan)
9. Re: Enum for natural numbers (Isaac Dupree)
----------------------------------------------------------------------
Message: 1
Date: Sun, 20 Dec 2009 14:59:45 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Sonntag 20 Dezember 2009 14:36:06 schrieb [email protected]:
> 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?
------------------------------
Message: 2
Date: Sun, 20 Dec 2009 16:34:11 +0200
From: Volkan YAZICI <[email protected]>
Subject: [Haskell-beginners] Re: Pure Regex.PCRE Possible? (PLEAC, Fix
Style)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On Sat, 19 Dec 2009, Volkan YAZICI <[email protected]> writes:
> I'm trying to implement FixStyle[1] program of PLEAC[2] for Haskell.
> (It's missing in the Haskell version of PLEAC.) For this purpose, I
> needed regular expressions. (Now, I have two problems!) Despite, my
> code is, IMHO, quite purely functional, Regex.PCRE.String functions
> totally mess up the design with impure functions they introduce even for
> really simple string matching stuff.
>
> If you'd check out the sources[3], you'll see below impure functions.
>
> transDictRegex :: IO PCRE.Regex
> matchRegex :: String -> IO (String, String, String)
> translate :: String -> IO String
>
> However, if I'm not mistaken, above functions _should_ be absolutely
> pure. Is it possible to encapsulate impure PCRE functions into their
> pure equivalents?
>
> BTW, why are PCRE functions impure?
OTH, I'm looking at my "translate" and it fails on infinite strings. Is
it because of something lacking in my implementation, or a limitation of
Regex.PCRE?
Regards.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: FixStyle.hs
Type: text/x-haskell
Size: 2113 bytes
Desc: not available
Url :
http://www.haskell.org/pipermail/beginners/attachments/20091220/f30cf389/FixStyle-0001.bin
------------------------------
Message: 3
Date: Sun, 20 Dec 2009 15:52:06 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Re: Pure Regex.PCRE Possible? (PLEAC,
Fix Style)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-15"
Am Sonntag 20 Dezember 2009 15:34:11 schrieb Volkan YAZICI:
>
> OTH, I'm looking at my "translate" and it fails on infinite strings. Is
> it because of something lacking in my implementation, or a limitation of
> Regex.PCRE?
>
>
> Regards.
I don't know whether the regex library can deal well with infinite input, but in
translate input = do
(head, word, tail) <- matchRegex input
tailTrans <- (translate tail)
return $ head ++ (transWord word) ++ tailTrans
the IO-semantics require that the whole input is processed before anything is
returned
(translating tail might throw an exception, after all). Maybe it'll work with a
little
unsafeInterleaveIO magic:
import System.IO.Unsafe
translate input = do
(head, word, tail) <- matchRegex input
tailTrans <- unsafeInterleaveIO (translate tail)
return $ head ++ (transWord word) ++ tailTrans
------------------------------
Message: 4
Date: Sun, 20 Dec 2009 16:16:32 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Re: Pure Regex.PCRE Possible? (PLEAC,
Fix Style)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-15"
Am Sonntag 20 Dezember 2009 15:52:06 schrieb Daniel Fischer:
> Am Sonntag 20 Dezember 2009 15:34:11 schrieb Volkan YAZICI:
> > OTH, I'm looking at my "translate" and it fails on infinite strings. Is
> > it because of something lacking in my implementation, or a limitation of
> > Regex.PCRE?
> >
> >
> > Regards.
>
> I don't know whether the regex library can deal well with infinite input,
No, it can't. It uses withCStringLen, that's not capable of dealing with
infinite input.
------------------------------
Message: 5
Date: Sun, 20 Dec 2009 21:06:36 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
-------- Original-Nachricht --------
> Datum: Sun, 20 Dec 2009 14:59:45 +0100
> Von: Daniel Fischer <[email protected]>
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] Enum for natural numbers
> Am Sonntag 20 Dezember 2009 14:36:06 schrieb [email protected]:
> > 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
------------------------------
Message: 6
Date: Sun, 20 Dec 2009 21:28:21 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Sonntag 20 Dezember 2009 21:06:36 schrieb [email protected]:
> > 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.
------------------------------
Message: 7
Date: Sun, 20 Dec 2009 21:35:45 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
-------- Original-Nachricht --------
> Datum: Sun, 20 Dec 2009 21:28:21 +0100
> Von: Daniel Fischer <[email protected]>
> An: [email protected]
> Betreff: Re: [Haskell-beginners] Enum for natural numbers
> Am Sonntag 20 Dezember 2009 21:06:36 schrieb [email protected]:
> > > 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
> [email protected]
> 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
------------------------------
Message: 8
Date: Sun, 20 Dec 2009 21:39:39 +0100
From: Deniz Dogan <[email protected]>
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
2009/12/20 <[email protected]>:
>
> -------- Original-Nachricht --------
>> Datum: Sun, 20 Dec 2009 21:28:21 +0100
>> Von: Daniel Fischer <[email protected]>
>> An: [email protected]
>> Betreff: Re: [Haskell-beginners] Enum for natural numbers
>
>> Am Sonntag 20 Dezember 2009 21:06:36 schrieb [email protected]:
>> > > 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
------------------------------
Message: 9
Date: Sun, 20 Dec 2009 15:55:37 -0500
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
[email protected] wrote:
> -------- Original-Nachricht --------
>> Datum: Sun, 20 Dec 2009 21:28:21 +0100
>> Von: Daniel Fischer <[email protected]>
>> An: [email protected]
>> Betreff: Re: [Haskell-beginners] Enum for natural numbers
>
>> Am Sonntag 20 Dezember 2009 21:06:36 schrieb [email protected]:
>>>> 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
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 18, Issue 18
*****************************************