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

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

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@gmail.com> wrote:


On Fri, Nov 27, 2015 at 10:31 AM, Jeffrey Brown <jeffbrown.the@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@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




--
Jeffrey Benjamin Brown