
On Fri, Nov 06, 2009 at 05:22:04PM +0100, Matthias Guedemann wrote:
I wrote a converter from a textfile format to a csv file. In order to get more familiar with Haskell I wrote it using Parsec. At first I had some difficulties and the monadic version seemed easier, but now it works as Applicative version and Parsec seems really straightforward.
For me, the Applicative version looks more functional and the monadic version looks more imperative.
Indeed; thta's a good way of putting it. Applicative looks more functional since it corresponds to function application, but a special sort of function application that carries along some sort of "context".
It seems that Applicative is enough for Parsers (think I remember a citation somewhere), but does it have a real advantage?
The Monad interface gives strictly more power: it allows you to give names to intermediate results, and *decide what to do* later based on those intermediate results. With Applicative, the actions that you perform must be fixed ahead of time. For example, consider parsing a file which contains a positive integer, followed by that many letters. For example, 3xyz 12abcdefghijkl are two instances of this format. In order to parse this, a monadic interface is required, since the result of parsing the number must be used to decide how many things to parse after that. However, for *many* purposes, an Applicative parsing interface is all you need. And if Applicative is enough, it's usually nicer/more elegant than Monad. (And using the least powerful/most general thing that works for your purpose is usually good style anyway.) -Brent