***DHSPAM*** Re: [Haskell-cafe] Newbie Q: Monad 'fail' and 'error'
Juan Carlos Arevalo Baeza
jcab.lists at jcabs-rumblings.com
Wed Jun 6 13:39:03 EDT 2007
On Wed, 06 Jun 2007 09:29:09 -0700, Dmitri O.Kondratiev
<dokondr at gmail.com> wrote:
> Default 'fail' implementation in Monad class will be:
> *DivBy> divBy 5 0
> Loading package haskell98-1.0 ... linking ... done.
> *** Exception: user error (div by zero)
>
> And when explicitly defining monad as Maybe it will be different:
> *DivBy> divBy 5 0::Maybe Int
> Nothing
>
> I am curious if it is possible to 'cast' divBy to List, Identity, other
> monads? How?
It just works, and you already did it for Maybe. Just provide the type:
*Main> divBy 5 0::[Int]
[]
*Main> divBy 5 1::[Int]
[5]
Identity fails with a stack overflow, though (GHC 6.6):
*Main> divBy 5 0::Identity Int
*** Exception: stack overflow
*Main> fail "KK" :: Identity ()
*** Exception: stack overflow
*Main> divBy 5 1::Identity Int
*** Exception: stack overflow
Anyway...
JCAB
More information about the Haskell-Cafe
mailing list