Should I avoid using Monad fail?

后端 未结 3 1087
-上瘾入骨i
-上瘾入骨i 2021-02-05 00:53

I\'m fairly new to Haskell and have been slowly getting the idea that there\'s something wrong with the existence of Monad fail. Real World Haskell warns against its use (\"Once

3条回答
  •  面向向阳花
    2021-02-05 01:53

    In Haskell 1.4 (1997) there was no fail. Instead, there was a MonadZero type class which contained a zero method. Now, the do notation used zero to indicate pattern match failure; this caused surprises to people: whether their function needed Monad or MonadZero depended on how they used the do notation in it.

    When Haskell 98 was designed a bit later, they did several changes to make programming simpler to the novice. For example, monad comprehensions were turned into list comprehensions. Similarly, to remove the do type class issue, the MonadZero class was removed; for the use of do, the method fail was added to Monad; and for other uses of zero, a mzero method was added to MonadPlus.

    There is, I think, a good argument to be made that fail should not be used for anything explicitly; its only intended use is in the translation of the do notation. Nevertheless, I myself am often naughty and use fail explicitly, too.

    You can access the original 1.4 and 98 reports here. I'm sure the discussion leading to the change can be found in some email list archives, but I don't have links handy.

提交回复
热议问题