title: How to decorate a Tree in Haskell
body: <p>I want to tag each element of a tree with a different value (Int, for example sake). I managed to do this but the code is ugly as a beast and I don't know how to work with Monads yet.</p>
  
  <p>My take:</p>
  
  <pre><code>data Tree a = Tree (a, [Tree a])
  
  tag (Tree (x, l)) n = ((m, x), l')
   where (m,l') = foldl g (n,[]) l
          where g (n,r) x = let ff = tag x n in ((fst $ fst ff) +1, (Tree ff):r)
  </code></pre>
  
  <p>Do you know some better way?</p>
  
  <p><strong>EDIT:</strong>
  I just realized that the above foldl really is mapAccumL. So, here is a cleaned version of the above:</p>
  
  <pre><code>import Data.List (mapAccumL)
  
  data Tree a = Tree (a, [Tree a])
  
  tag (Tree (x, l)) n = ((m,x),l')
    where (m,l') = mapAccumL g n l
          g n x  = let ff@((f,_),_) = tag x n in (f+1,ff)
  </code></pre>