title: Mirror binary tree in Prolog
body: <p>What I have...</p>
  
  <pre><code>tree(nil).
  tree(b(Left,_,Right)) :-
      tree(Left),
      tree(Right).
  
  mirror(b(Left,Head,Right), NewTree) :-
      mirror(Left,NewLeft),
      mirror(Right,NewRight),
      NewTree = b(NewRight,Head,NewLeft).
  </code></pre>
  
  <p>What I'm querying...</p>
  
  <pre><code>mirror(b(nil,a,b(nil,b,nil)), Result).
  </code></pre>
  
  <p>Expected result </p>
  
  <pre><code>Result = b(b(nil,b,nil),a,nil).
  </code></pre>
  
  <p>The tree b(Left,Right,Head) is the first argument of mirror, NewTree is the goal. mirror(Left,NewLeft) recurses through the left side and yields the goal NewLeft, same for Right. NewTree is the tree b(NewRight,Head,NewLeft).</p>
  
  <p>I'm not sure why this isn't working could someone please help.</p>