Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've modified your types slightly. Study this code carefully:</p> <pre><code>import Control.Monad.State -- It's better not to use a pair as the argument of the constructor data Tree a = Tree a [Tree a] deriving Show -- We typically want to put the Tree argument last; it makes it -- easier to compose tree functions. -- -- Also, the Enum class is what you want here instead of numbers; -- you want a "give me the next tag" operation, which is the succ -- method from Enum. (For Int, succ is (+1).) tag :: Enum t =&gt; t -&gt; Tree a -&gt; Tree (a, t) tag init tree = -- tagStep is where the action happens. This just gets the ball -- rolling. evalState (tagStep tree) init -- This is one monadic "step" of the calculation. It assumes that -- it has access to the current tag value implicitcly. I'll -- annotate it in the comments. tagStep :: Enum t =&gt; Tree a -&gt; State t (Tree (a, t)) tagStep (Tree a subtrees) = do -- First, recurse into the subtrees. mapM is a utility function -- for executing a monadic action (like tagStep) on a list of -- elements, and producing the list of results. subtrees' &lt;- mapM tagStep subtrees -- The monadic action "get" accesses the implicit state parameter -- in the State monad. The variable tag gets the value. tag &lt;- get -- The monadic action `put` sets the implicit state parameter in -- the State monad. The next get will see the value of succ tag -- (assuming no other puts in between). -- -- Note that when we did mapM tagStep subtrees above, this will -- have executed a get and a put (succ tag) for each subtree. put (succ tag) return $ Tree (a, tag) subtrees' </code></pre> <hr> <p><strong>EDIT:</strong> Same solution as above, but put through one round of refactoring into reusable pieces:</p> <pre><code>-- This function is not part of the solution, but it will help you -- understand mapTreeM below. mapTree :: (a -&gt; b) -&gt; Tree a -&gt; Tree b mapTree fn (Tree a subtrees) = let subtrees' = map (mapTree fn) subtrees a' = fn a in Tree a' subtrees' -- Normally you'd write that function like this: mapTree' fn (Tree a subtrees) = Tree (fn a) $ map (mapTree' fn) subtrees -- But I wrote it out the long way to bring out the similarity to the -- following, which extracts the structure of the tagStep definition from -- the first solution above. mapTreeM :: Monad m =&gt; (a -&gt; m b) -&gt; Tree a -&gt; m (Tree b) mapTreeM action (Tree a subtrees) = do subtrees' &lt;- mapM (mapTreeM action) subtrees a' &lt;- action a return $ Tree a' subtrees' -- That whole business with getting the state and putting the successor -- in as the replacement can be abstracted out. This action is like a -- post-increment operator. postIncrement :: Enum s =&gt; State s s postIncrement = do val &lt;- get put (succ val) return val -- Now tag can be easily written in terms of those. tag init tree = evalState (mapTreeM step tree) init where step a = do tag &lt;- postIncrement return (a, tag) </code></pre> <p>You can make <code>mapTreeM</code> process the local value before the subtrees if you want:</p> <pre><code>mapTreeM action (Tree a subtrees) = do a' &lt;- action a subtrees' &lt;- mapM (mapTreeM action) subtrees return $ Tree a' subtrees' </code></pre> <p>And using <code>Control.Monad</code> you can turn this into a one-liner:</p> <pre><code>mapTreeM action (Tree a subtrees) = -- Apply the Tree constructor to the results of the two actions liftM2 Tree (action a) (mapM (mapTreeM action) subtrees) -- in the children-first order: mapTreeM' action (Tree a subtrees) = liftM2 (flip Tree) (mapM (mapTreeM action) subtrees) (action a) </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. 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.
 

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