title: Non-trivial Prolog find and replace
body: <p>So we can easily find and replace an atom with another atom in Prolog by doing something like:</p>
  
  <pre><code>replace([],A,B,[]).
  replace([H|T],A,B,[B|Result]) :- 
      H=A, 
      replace(T,A,B,Result),!.
  replace([H|T],A,B,[H|Result]) :- 
      replace(T,A,B,Result).
  </code></pre>
  
  <p>I'm sure there are other ways to do this too.</p>
  
  <p>However, I want to do something more complicated in logic in computing.  How would you do something like replacing conjunctions like <code>conj(x,y)</code> in a logical statement with just (x,y)?  So it's like final and replace but not with atoms.  So we could have something like <code>reduce(conj(conj(x,y),z)).</code> that I would want reducing to <code>((x,y),z)</code>.  </p>
  
  <p>This is a simple example with only conjunctions but this is what I want to happen in the case of conjunctions.  If anyone's interested, this is all about descriptive logic and the tableau method.</p>
  
  <p>What I'm confused about it how you go about doing a find and replace when the input isn't actually a list; it's a structure.  I don't see how you can solve this without using the standard <code>[H|T]</code> trick with recursion and lists.  Has anyone got any ideas?</p>
  
  <p>Many thanks.</p>