[Haskell-beginners] Re: Monadic composition without throwing genericity under the bus?

Heinrich Apfelmus apfelmus at quantentunnel.de
Thu Feb 4 05:46:28 EST 2010


Dave Bayer wrote:
> I've been playing with parsing, for text filtering applications such
> as an alternate GHC literate preprocessor. Here, it pays to have one's
> monad handle both the input and output streams out of sight. Using
> ShowS-valued monads, one can express most grammatical constructs as
> simple composition. I'm sure many people have had this idea, but the
> resulting parsers that I write end up much shorter than any demo code
> I've seen. For example, the "code is indented, comments are flush,
> periods delimit block comments" literate preprocessor that I depend on
> daily (leaving out the heredoc code) is just
>
>   dot, comment, dotLine, commentLine, codeLine, dotBlock, delit ∷ Parser
> 
>   dot = char '.'
>   comment = place "-- "
> 
>   codeLine = white ∘ till (heredoc ∨ whiteLine) word
>   commentLine = comment ∘ line
> 
>   dotLine = comment ∘ dot ∘ whiteLine
>   dotBlock = dotLine ∘ till (dotLine ∨ eof) (whiteLine ∨ commentLine)
> 
>   delit = till (skip (many whiteLine) ∘ eof)
>     (whiteLine ∨ dotBlock ∨ codeLine ∨ commentLine)

This looks intriguing! Can you elaborate on how this works?


Regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com



More information about the Beginners mailing list