title: Replacing an element in a n-ary tree
body: <p>I'm pretty new to Haskell and still have some problems getting my head around functional programming. With that said:</p>
  
  <p>I have a custom n-ary tree datatype</p>
  
  <pre><code>data Tree = Empty | Leaf String | Node String [Tree]
  </code></pre>
  
  <p>I'm trying to write a function to replace an element in a tree, i.e. </p>
  
  <pre><code>replaceInTree :: String -&gt; String -&gt; Tree -&gt; Maybe Tree
  </code></pre>
  
  <p>Replacing the first string with the second. There is only ever one occurance of each string so I can stick with the first one found.
  I've made a few efforts but I can't grasp how to reconstruct the full tree after replacing the element. Trivially, I have this:</p>
  
  <pre><code>ntreeReplace x y (Node z lis)
      |(x==z) = Just (Node y lis) 
  </code></pre>
  
  <p>which only changes the head <code>node</code>, obviously. I've written a function that returns true if an element is present in the tree, as a <code>leaf</code> or <code>node</code>, but progress beyond that is proving difficult.</p>
  
  <hr>
  
  <p>Thanks for any help!</p>