
In parsing libraries for Haskell the "parse" function (or its equivalent)
typically returns an Either. For instance there's
parse :: Stream
http://hackage.haskell.org/package/parsec-3.1.6/docs/Text-Parsec-Prim.html#t...
s Identity
http://hackage.haskell.org/package/transformers-0.4.1.0/docs/Data-Functor-Id...
t
=> Parsec
http://hackage.haskell.org/package/parsec-3.1.6/docs/Text-Parsec-Prim.html#t...
s
() a -> SourceName
http://hackage.haskell.org/package/parsec-3.1.6/docs/Text-Parsec-Pos.html#t:...->
s -> Either
http://hackage.haskell.org/package/base-4.7.0.1/docs/Data-Either.html#t:Eith...
ParseError
http://hackage.haskell.org/package/parsec-3.1.6/docs/Text-Parsec-Error.html#...
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
On Fri, Nov 27, 2015 at 10:31 AM, Jeffrey Brown
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