[Haskell-cafe] tips on using monads
Claus Reinke
claus.reinke at talk21.com
Mon May 18 12:58:50 EDT 2009
>>> I've got one of those algorithms which "threatens to march off the right edge" (in the words of
>>> Goerzen et al). I need something like a State or Maybe monad, but this is inside the IO monad.
>>> So I presume I need StateT or MaybeT. However, I'm still (sdlowly) learning about monads from
>>> first principles. I thought I might present my code and get some pointers... maybe someone could
>>> actually show me how to rewrite it, which would be a neat way to see MaybeT and StateT in
>>> action. I'm hoping to get anything from a one-line response to a rewrite of my code. Anything
>>> will help.
>>
>> Perhaps this is useful:
>> http://www.haskell.org/haskellwiki/Equational_reasoning_examples#Coding_style:_indentation_creep_with_nested_Maybe
> I can't quite tell--is that example in the IO monad? Part of my difficulty is that I'm inside IO.
> I know how to do this with Maybe, except that I have to combine Maybe and IO (use MaybeT?)
It was in the GHC.Conc.STM monad, so yes, it used a MaybeT
and Control.Monad.Trans.MonadTrans's lift (btw, the MonadTrans
docs only point to [1], but [2] might also be of interest, if rather
more compact/terse).
Claus
[1] http://web.cecs.pdx.edu/~mpj/pubs/springschool.html
[2] http://web.cecs.pdx.edu/~mpj/pubs/modinterp.html
More information about the Haskell-Cafe
mailing list