[Haskell-beginners] comment on this debugging trick?

Jeffrey Brown jeffbrown.the at gmail.com
Fri Nov 27 22:51:45 UTC 2015

In parsing libraries for Haskell the "parse" function (or its equivalent)
typically returns an Either. For instance there's

parse :: Stream
 s Identity
=> Parsec
() a -> SourceName
s -> Either

in Text.Parsec.Prim. This is an example of making invalid state impossible.
parse could return a list of all possible parses, with the empty list
signifying that parsing failed. But by using an Either, the case of failure
can be distinguished as a Left. Haskell will know to treat the Left
differently, and if you need an error report, that left can bubble (through
multiple Either-returning functions, with some handy do-notation) up to the
user without ever invoking an exception to the ordinary control flow.

On Fri, Nov 27, 2015 at 12:51 PM, Dennis Raddle <dennis.raddle at gmail.com>

> On Fri, Nov 27, 2015 at 10:31 AM, Jeffrey Brown <jeffbrown.the at gmail.com>
> wrote:
>> Elliot Cameron, very smart guy, once advised me to make illegal state
>> invalid. Jake Brownson, another, recommended trying to use Maybes and
>> Eithers instead of throwing exceptions. I'm having trouble coming up with
>> examples but I think they're both helpful.
> I think I know roughly what you are talking about, but there are places
> where unexpected input might result in something like an empty list.
> Suppose I need the head of that list under normal conditions. Maybe the
> list is produced by library code so I can't alter the data structures.
> That's the kind of situation I want to identify precisely and immediately.
> D
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Jeffrey Benjamin Brown
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151127/e9d28423/attachment.html>

More information about the Beginners mailing list