Note that there are some explanatory texts on larger screens.

plurals
  1. POPropogation of State Monad
    primarykey
    data
    text
    <p>I have the following function for walking around "edges" of the "graph" of my game world. It alters the state of the world--specifically, the player's location. I need to report a message alerting the player of their change in location as well. </p> <p>So I could either return a tuple of (message, newWorld), or I could use a State monad. (Right? I'm new to this stuff.)</p> <p>Here's my attempt at the monad approach:</p> <pre><code>walk dir = do world &lt;- get let attempt = filter (\e -&gt; edgeDirection e == dir) $ edges edges = (worldEdges world) M.! playerLoc playerLoc = playerLocation $ worldPlayer world case attempt of [] -&gt; return "You can't go that way." (edge : _) -&gt; do put world' return message where world' = world { worldPlayer = player' } player' = (worldPlayer world) { playerLocation = loc } loc = edgeLocation edge message = "You go " ++ (downcase $ show dir) </code></pre> <p>The thing is, now the function that <em>calls</em> <code>walk</code> has a State monad to deal with. Do I <code>runState</code> it right there, or do I have that function use State monads too, like this:</p> <pre><code>parseWalk dir = let direction = case dir of ('e' : _) -&gt; Just East ('n' : _) -&gt; Just North ('s' : _) -&gt; Just South ('w' : _) -&gt; Just West ('u' : _) -&gt; Just Up ('d' : _) -&gt; Just Down _ -&gt; Nothing in case direction of Just d -&gt; walk d Nothing -&gt; return "You can't go that way" </code></pre> <p>Then what about <code>parseWalk</code>'s caller, and so forth? Should I try to keep this State monad passed around as long as possible? Should I be seeing a lot of <code>MonadState ...</code> in my type signatures?</p> <p>Sincerely,</p> <p>Confused in California</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload