Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a pseudocodey sketch of how you might use the <a href="http://hackage.haskell.org/packages/archive/mtl/2.0.1.0/doc/html/Control-Monad-State-Lazy.html"><code>State</code></a> monad to thread the search state through the computation:</p> <pre><code>import Control.Monad.State type SearchState = ... type Move = ... type Fitness = ... determineMoves :: State SearchState [Move] determineMoves = do -- since determineMoves is in the State monad, we can grab the state here st &lt;- get ... evaluateMoves :: [Move] -&gt; [(Move, Fitness)] evaluateMoves = ... chooseMove :: [(Move, Fitness)] -&gt; Move chooseMove = ... -- makeMove is not itself monadic, but operates on the SearchState -- type we're threading through with the State monad makeMove :: Move -&gt; SearchState -&gt; SearchState makeMove m st = ... loop :: State SearchState () loop = do moves &lt;- determineMoves let candidates = evaluateMoves moves move = chooseMove candidates -- we pass a function (SearchState -&gt; SearchState) to modify in -- order to update the threaded SearchState modify (makeMove move) loop </code></pre> <p>Notice that even though your main computation is in the state monad, not every component has to be in the monad. Here, <code>evaluateMoves</code> and <code>chooseMove</code> are non-monadic, and I've used <code>let</code> to show you how to explicitly integrate them into a <code>do</code> block. Once you get comfortable with this style, though, you'll probably want to get comfortable using <code>&lt;$&gt;</code> (aka <code>fmap</code>) and function composition to get more succinct:</p> <pre><code>loop :: State SearchState () loop = do move &lt;- (chooseMove . evaluateMoves) &lt;$&gt; determineMoves modify (makeMove move) loop </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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