[Haskell-beginners] State Monad: how to use other Stateful computation on sub-state inside
martin.drautzburg at web.de
Sat Oct 17 13:58:53 UTC 2015
Am 10/17/2015 um 10:01 AM schrieb martin:
> This is quite insightful. Now the problem presents itself as follows:
> In fact I orginally started with plain itmAdd (the one you called itmAdd'') which has no business with State or System.
> It is just Item->ItemDb -> ItemDb.
> However, I wanted to use do-notation and made it monadic leading to item' :: Item->State ItemDb ()
> But then it becomes difficult to use it inside State System. Currently I am using
> subState :: (s1 -> s2) -> State s2 a -> State s1 s2
> subState accessor f = do
> s <- get
> return $ execState f $ accessor s
> to make the necessary transformation. I have the feeling that this way of stacking things is a bit inside-out. And as
> you say, it only hides the ugliness. I will try to stack things bottom up.
This turned out okay. Thank you very much and many thanks to Karl Voelker too, though I am still shying away from lenses.
More information about the Beginners