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. writing many1Till combinator for Parsec (Vlad Skvortsov)
2. Re: How to create a monad instance (Maciej Piechotka)
3. Re: Printing the bits of an Int|Double (M Xyz)
4. Re: Re: How to create a monad instance (Marco De Oliveira)
5. Problem implementing a stack (Jacob Faren)
6. Re: Problem implementing a stack (Chadda? Fouch?)
7. Re: writing many1Till combinator for Parsec (Daniel Fischer)
----------------------------------------------------------------------
Message: 1
Date: Fri, 11 Dec 2009 16:58:11 -0800
From: Vlad Skvortsov <[email protected]>
Subject: [Haskell-beginners] writing many1Till combinator for Parsec
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Hi,
I'm writing a parser where I find myself in need to use manyTill
combinator with an additional constraint that there should be at least
one meaningful element before the trailer (say, a word ended with a
period: 'abc.').
Here is what I have:
many1Till :: Parser a -> Parser end -> Parser [a]
many1Till p end = do
p1 <- p
ps <- manyTill p end
return (p1:ps)
The problem here is that I want to catch and report a case when 'p1'
could be actually parsed by 'end' ('..' is not a word ended by a
period). Generally 'p' and 'end' can parse the same subset of strings.
Another version a had was:
many1Till :: Parser a -> Parser end -> Parser [a]
many1Till p end = do
try (end >> (unexpected "sequence terminator"))
<|> (do { p1 <- p; ps <- manyTill p end; return (p1:ps) })
Here there are two disadvantages:
1) I don't like hardcoding "sequence terminator" here;
2) the error output should say that only 'p' parser is expected, while
it says (technically correct) that either 'p' or 'end' is expected:
Prelude Main Text.ParserCombinators.Parsec> parseTest (many1Till letter
(char '.')) "1"
parse error at (line 1, column 1):
unexpected "1"
expecting "." or letter
(What I want here is to say "expecting letter")
Any suggestions?
Thanks!
--
Vlad Skvortsov, [email protected], http://vss.73rus.com
------------------------------
Message: 2
Date: Sat, 12 Dec 2009 01:38:13 +0000
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: How to create a monad instance
To: [email protected]
Message-ID: <1260581893.2597.60.ca...@picard>
Content-Type: text/plain; charset="UTF-8"
On Sat, 2009-12-12 at 01:17 +0100, Marco De Oliveira wrote:
> Hi,
>
> Is it possible to create an instance of Monad for IOException?
> Each time a try I stay blocked by IO monad.
>
> with the definition:
>
> data IOException a = IOExceptionCons (IO (Exception a))
>
> data Exception a = SuccessCons (Maybe Warning) a
> | ErrorCons Error
>
> data Warning = WarningCons1
> | WarningCons2
>
> data Error = ErrorCons1
> | ErrorCons2
>
> Regards
Yes - to begin with:
newtype IOException a = IOException {runIOException :: IO (Exception a)}
data Exception a = Success (Maybe Warning) a
| Exception Error
data Warning = Warning1
| Warning2
data Error = Error1
| Error2
instance Monad Exception where
return = Success Nothing
(Success w v) >>= f = f v
-- (Success w v) >>= f = case f v of
-- Success _ v' -> Success w v'
-- Exception e -> Exception e
(Exception e) >>= _ = Exception e
instance Monad IOException where
return = IOException . return . return
m >>= f = IOException $ runIOException m >>= runIOException . f
However:
1. What should be the combination of warnings I'd rather threat them as
some 'proper' monoid (for ex. List instead of Maybe)
2. It is much easier to use mtl:
type IOException = ErrorT (Either Error) (WriterT [Warning] IO)
Regards
------------------------------
Message: 3
Date: Fri, 11 Dec 2009 18:46:22 -0800 (PST)
From: M Xyz <[email protected]>
Subject: Re: [Haskell-beginners] Printing the bits of an Int|Double
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
Thanks Stephen for your response.
With the help of #haskell I was actually able to find my answer:
import Char
import Numeric
import Data.Bits
import Data.Binary.Put
import Data.Binary.IEEE754
import qualified Data.ByteString.Lazy as LBS
main = do putStrLn . show $ map getBits (LBS.unpack . runPut $ putFloat64be
4.123)
getBits i = showIntAtBase 2 (chr . (48+)) i ""
--- On Fri, 12/11/09, Stephen Tetley <[email protected]> wrote:
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Printing the bits of an Int|Double
To: "M Xyz" <[email protected]>
Cc: [email protected]
Date: Friday, December 11, 2009, 9:29 AM
Further...
If you need double precision you might have to do it yourself.
In absence of comments in the code I posted, the algorithm I used for
single precision is from here:
http://www.utdallas.edu/~cantrell/ee6481/lectures/float_comp.pdf
Eventually I'll get around to double precision, but not soon.
Best wishes
Stephen
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20091211/611c18ed/attachment-0001.html
------------------------------
Message: 4
Date: Sat, 12 Dec 2009 11:18:51 +0100
From: Marco De Oliveira <[email protected]>
Subject: Re: [Haskell-beginners] Re: How to create a monad instance
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
2009/12/12 Maciej Piechotka <[email protected]>:
> On Sat, 2009-12-12 at 01:17 +0100, Marco De Oliveira wrote:
>> Hi,
>>
>> Is it possible to create an instance of Monad for IOException?
>> Each time a try I stay blocked by IO monad.
>>
>> with the definition:
>>
>> data IOException a = IOExceptionCons (IO (Exception a))
>>
>> data Exception a = SuccessCons (Maybe Warning) a
>> | ErrorCons Error
>>
>> data Warning = WarningCons1
>> | WarningCons2
>>
>> data Error = ErrorCons1
>> | ErrorCons2
>>
>> Regards
>
> Yes - to begin with:
>
> newtype IOException a = IOException {runIOException :: IO (Exception a)}
>
> data Exception a = Success (Maybe Warning) a
> | Exception Error
>
> data Warning = Warning1
> | Warning2
> data Error = Error1
> | Error2
>
> instance Monad Exception where
> return = Success Nothing
> (Success w v) >>= f = f v
> -- (Success w v) >>= f = case f v of
> -- Success _ v' -> Success w v'
> -- Exception e -> Exception e
> (Exception e) >>= _ = Exception e
> instance Monad IOException where
> return = IOException . return . return
> m >>= f = IOException $ runIOException m >>= runIOException . f
>
> However:
> 1. What should be the combination of warnings I'd rather threat them as
> some 'proper' monoid (for ex. List instead of Maybe)
> 2. It is much easier to use mtl:
> type IOException = ErrorT (Either Error) (WriterT [Warning] IO)
>
> Regards
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
Thanks for your help.
I have try your code ghci and I think there is a mistake in the
definition of (>>=) operator.
test1 m f = IOException $ runIOException m >>= runIOException . f
:t test1
test1 :: IOException a
-> (Exception a -> IOException a)
-> IOException a
I think the defintion is:
test :: IOException a -> ( a -> IOException a) -> IOException a
1. why I am not using monoid? I try to bind C code and the function
return only one warning. But if I change i little the structure of
IOException, it may have sense to use a List.
newtype IOException a = IOException {runIOException :: ([Warning] ->
IO (Exception a))}
2. Sorry but I have a lot of difficulties to understand the concept of
Monad Transformers
------------------------------
Message: 5
Date: Tue, 8 Dec 2009 20:17:25 -0500
From: Jacob Faren <[email protected]>
Subject: [Haskell-beginners] Problem implementing a stack
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hello. I'm implement data structures (a stack, here) to get familiar with
Haskell. It's not going very well. I have the following code...
-- test.hs
data Stack a = Top (Stack a) | Layers [a] deriving (Show)
stack :: Stack a
stack = Top (Layers [])
push :: Stack a -> a -> Stack a
push (Top (Layers ls)) value = Top (Layers (value:ls))
And I run the following in ghci...
Prelude> :l test
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> push stack 4
Top (Layers [4])
*Main> let st = stack
*Main> push st 4
^CInterrupted.
The output of the last command never resolves. I have to kill it. Might
anyone have an idea why the push function is failing with st, but not
with stack?
I know there are other stack implementations that work, but I really
want to know what's going on here.
Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20091208/dbf45886/attachment-0001.html
------------------------------
Message: 6
Date: Sat, 12 Dec 2009 12:03:05 +0100
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] Problem implementing a stack
To: Jacob Faren <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Wed, Dec 9, 2009 at 2:17 AM, Jacob Faren <[email protected]> wrote:
> Hello. I'm implement data structures (a stack, here) to get familiar with
> Haskell. It's not going very well. I have the following code...
>
> Â Â Â -- test.hs
> Â Â Â data Stack a = Top (Stack a) | Layers [a] deriving (Show)
>
> Â Â Â stack :: Stack a
> Â Â Â stack = Top (Layers [])
>
> Â Â Â push :: Stack a -> a -> Stack a
> Â Â Â push (Top (Layers ls)) value = Top (Layers (value:ls))
>
> And I run the following in ghci...
>
> Â Â Â Prelude> :l test
>    [1 of 1] Compiling Main            ( test.hs, interpreted
> )
> Â Â Â Ok, modules loaded: Main.
> Â Â Â *Main> push stack 4
> Â Â Â Top (Layers [4])
> Â Â Â *Main> let st = stack
> Â Â Â *Main> push st 4
> Â Â Â ^CInterrupted.
>
> The output of the last command never resolves. I have to kill it. Might
> anyone have an idea why the push function is failing with st, but not
> with stack?
It shouldn't, and it doesn't here, which version of GHC are you using ?
By the way, your Stack datatype seems broken to me, what would be the
meaning of (Top (Top (Top (Layers []))) ? I do believe that you do not
wish to allow anything besides a Layers inside a Top, and your push
function supports this interpretation. In this case, your Layers
constructor is unnecessary and your datatype declaration should be :
> data Stack a = Top [a] deriving (Show)
At which point you may realize that this type is isomorphic to the
list type (+ one bottom) and thus is better written :
- Either as a type synonym :
type Stack a = [a]
- Or if you wish to ensure some invariants on your type or add some
typeclass instances specific to stacks :
newtype Stack a = Top [a] deriving (Show)
--
Jedaï
------------------------------
Message: 7
Date: Sat, 12 Dec 2009 14:16:12 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] writing many1Till combinator for
Parsec
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Samstag 12 Dezember 2009 01:58:11 schrieb Vlad Skvortsov:
> Hi,
>
> I'm writing a parser where I find myself in need to use manyTill
> combinator with an additional constraint that there should be at least
> one meaningful element before the trailer (say, a word ended with a
> period: 'abc.').
>
> Here is what I have:
>
> many1Till :: Parser a -> Parser end -> Parser [a]
> many1Till p end = do
notFollowedBy end
> p1 <- p
> ps <- manyTill p end
> return (p1:ps)
>
You want a nonempty sequence of 'p's which aren't 'end's, followed by an 'end'.
So you could do for example
a)
many1Till p end = do
ps <- manyTill p end
guard (not $ null ps)
return ps
, i.e. check whether the result of manyTill is a nonempty list after the fact,
or check
whether manyTill p end will return an empty list before manyTill is run, like
b)
many1Till p end = do
notFollowedBy end
manyTill p end
(i.e. many1Till p end = notFollowedBy end >> manyTill p end).
> The problem here is that I want to catch and report a case when 'p1'
> could be actually parsed by 'end' ('..' is not a word ended by a
> period). Generally 'p' and 'end' can parse the same subset of strings.
>
> Another version a had was:
>
> many1Till :: Parser a -> Parser end -> Parser [a]
> many1Till p end = do
> try (end >> (unexpected "sequence terminator"))
> <|> (do { p1 <- p; ps <- manyTill p end; return (p1:ps) })
>
> Here there are two disadvantages:
>
> 1) I don't like hardcoding "sequence terminator" here;
> 2) the error output should say that only 'p' parser is expected, while
> it says (technically correct) that either 'p' or 'end' is expected:
>
> Prelude Main Text.ParserCombinators.Parsec> parseTest (many1Till letter
> (char '.')) "1"
> parse error at (line 1, column 1):
> unexpected "1"
> expecting "." or letter
>
> (What I want here is to say "expecting letter")
For that, you need the slightly clumsier
c)
many1Till p end = do
notFollowedBy end
p1 <- p
ps <- manyTill p end
return (p1:ps)
>
> Any suggestions?
>
> Thanks!
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 18, Issue 10
*****************************************