[Haskell-cafe] Combining State and List Monads

Paolino paolo.veronelli at gmail.com
Sat Aug 25 07:50:08 CEST 2012


Hello,

the state should be inside, to count, so type is ListT (State Int) (Int,Int)

the runner is then runState (runListT countCalls) 0

but [] is not of type ListT m so you need to wrap it in ListT . return

import Control.Monad.List
import Control.Monad.State
import Control.Monad.Instances

countCalls :: ListT (State Int) (Int,Int)
countCalls = do
  a <- ListT . return $ [1..2]
  b <- ListT . return $ [1..2]
  modify (+1)
  return (a,b)

regards

paolino

2012/8/25 Henry Laxen <nadine.and.henry at pobox.com>

> Dear Cafe,
>
> It seems to me there should be some simple way of doing this, but thus
> far it eludes me.  I am trying to combine the State and List monads to
> do the following:
>
> countCalls = do
>   a <- [1..2]
>   b <- [1..2]
>   modify (+1)
>   return (a,b)
>
>
> where with some combination of ListT, StateT, List, State, or who
> knows what would result in:
>
> ([(1,1),(1,2),(2,1),(2,2)],4)
>
> assuming we initialize the state to 0
>
> Is there any way to make this happen?
> Thanks in advance.
>
> Henry Laxen
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120825/7b1d0a78/attachment.htm>


More information about the Haskell-Cafe mailing list