[prev in list] [next in list] [prev in thread] [next in thread]
List: haskell-beginners
Subject: Re: [Haskell-beginners] parse block comments
From: mike h <mike_k_houghton () yahoo ! co ! uk>
Date: 2017-03-18 22:49:11
Message-ID: AB704F77-CE0B-4EC5-B0FA-A1B357F6099C () yahoo ! co ! uk
[Download RAW message or body]
Monadic and applicative.
blockCmnt :: Parser ()
blockCmnt = do
string "{-"
manyTill item (string "-}")
return ()
manyTill p endp = scan where
scan = do
endp
return []
<|>
do
x <- p
xs <- scan
return (x:xs)
blockCmnt' :: Parser ()
blockCmnt' = string "{-" >> manyTill item (string "-}") >> return ()
manyTill' p endp = scan' where
scan' = endp *> return [] <|> pure (:) <*> p <*> scan'
Thanks.
M
> On 18 Mar 2017, at 21:47, Francesco Ariis <fa-ml@ariis.it> wrote:
>
> On Sat, Mar 18, 2017 at 09:11:20PM +0000, mike h wrote:
>> Hi,
>>
>> Below is code I'm building up for simple monadic and applicative parsers
>> from first principles.
>
> Hello Mike,
> You might want to check `manyTill` from Parsec to get an idea:
>
> manyTill :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
> manyTill p end = scan
> where
> scan = do { end; return [] } <|>
> do { x <- p; xs <- scan; return (x:xs) }
>
>
> The 'trick' lies in <|> (alternative). Does that help?
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic