[Haskell-cafe] Re: not possible with monad transformers ?
Pavel Zolnikov
pavelzolnikov at yahoo.com
Tue Nov 30 13:36:46 EST 2004
Thanks for response; unfortunately my problem was a bit deeper.
Note to self: try to verify all code snippets before posting ;-)
The problem is that code on line 4 is useless.
If one of the arguments a or b is Nothing, computation will just return
Nothing and will not bother to execute lines 4-5:
1 foo a b = do
2 output "before"
3 let r = liftM2 (+) a b
4 when (r == Nothing) $ output "error"
5 return r -- ??? "lift r"
I guess it does make perfect sense. It just was not intuitive to me
that as soon as I use Maybe monad in my monad transformation I will
loose ability to do output *after* error occurred.
But, anyway, I think I was able to find a nice solution to that:
type M2 a = OuptutMonadT Maybe String a
whenError:: M2 a -> M2 a -> M2 a …
1 foo a b = do
2 output "before"
3 let r = liftM2 (+) a b
4 `whenError` $ reportError "error"
5 return r
whenError combines two computations so that if first fails it will use
second instead.
Thanks,
Pavel.
More information about the Haskell-Cafe
mailing list