[Haskell-beginners] Maybe, Either

Alexander Dunlap alexander.dunlap at gmail.com
Tue Sep 15 18:33:25 EDT 2009


On Tue, Sep 15, 2009 at 8:56 AM, Michael Snoyman <michael at snoyman.com> wrote:
>
>
> On Tue, Sep 15, 2009 at 6:21 AM, Brandon S. Allbery KF8NH
> <allbery at ece.cmu.edu> wrote:
>>
>> On Sep 14, 2009, at 14:42 , Michael Snoyman wrote:
>>
>> I understand that fail being in Monad is controversial, but my version of
>> the function works in *all* monads. This is very
>>
>> Not really; "fail" in non-MonadPlus-es is a rather poorly defined notion,
>> and there are no guarantees that the result will be at all sane.  "mzero" is
>> well defined.
>
> mzero also does not allow giving error messages. There are times when you
> want to be able to fail with an explanation of why. fail seems to fit the
> bill properly for this (fail taking a String argument and all...).

In general, I think this is a bad idea. Except for short, throwaway
code, Strings should not be used to encode errors simply because if
your error message is a String, client code basically has no choice
but to show it to the user and probably exit: since it's quite hard
for the code to parse the String back in to find out what the error
message was, it's hard for client code to recover from the error. If
you have multiple types of errors from a single function, it's better
to define your own error type with constructors representing different
types of errors so client code can figure out what went wrong. (And in
many cases, I have found that if a function can fail in multiple ways,
I am trying to do too much in one function. There are, of course,
numerous exceptions.)

Alex


More information about the Beginners mailing list