<br><div><span class="gmail_quote">On 10/20/07, <b class="gmail_sendername">Andrew Coppin</b> &lt;<a href="mailto:andrewcoppin@btinternet.com">andrewcoppin@btinternet.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I&#39;m writing some code where I take an expression tree and transform it<br>into another equivilent one.<br><br>Now it&#39;s moderately easy to write the code that does the transformation.<br>But what I *really* want is to print out the transformation *sequence*.
<br>This appears to be much more awkward.<br><br>What I have is a function like this:<br><br>&nbsp;&nbsp;transform :: Expression -&gt; [Expression]<br><br>The trouble is, if you want to apply the transformation recursively,<br>things get very messy. Oh, it *works* and everything. It&#39;s just really
<br>messy and verbose. In Haskell, this is usually a sign that you want to<br>start applying some ingenious trickery... but I&#39;m having an ingeniety<br>failure here.<br><br>Suppose, for example, that in one case you want to recursively transform
<br>two subexpressions. I end up writing something like<br><br>&nbsp;&nbsp;transform (...sub1...sub2...) =<br>&nbsp;&nbsp;&nbsp;&nbsp;let<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub1s = transform sub1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub2s = transform sub2<br>&nbsp;&nbsp;&nbsp;&nbsp;in map (\sub1&#39; -&gt; put sub1&#39; back into main expression) sub1s ++ map
<br>(\sub2&#39; -&gt; put sub2&#39; back into main expression) sub2s<br><br>After you&#39;ve typed that a few times, it becomes *very* boring! But I<br>can&#39;t think of a clean way to abstract it. :-(<br><br>It&#39;s *almost* like you want to use the list monad:
<br><br>&nbsp;&nbsp;transform (...sub1...sub2...) = do<br>&nbsp;&nbsp;&nbsp;&nbsp;sub1&#39; &lt;- transform sub1<br>&nbsp;&nbsp;&nbsp;&nbsp;sub2&#39; &lt;- transform sub2<br>&nbsp;&nbsp;&nbsp;&nbsp;return (put sub1&#39; and sub2&#39; back into the main expression)<br><br>Except that that doesn&#39;t quite work properly. As shown above, I actually
<br>want to go through all the transformation steps for the first branch,<br>and *then* all the steps for the second branch.<br><br>Any hints?</blockquote><div><br>Hmm... I&#39;m having trouble understanding exactly what you want.&nbsp; In particular, I don&#39;t understand what this statement:
<br><br>&quot;But what I *really* want is to print out the transformation *sequence*.&quot;<br><br>has to do with the pseudocode that you exhibit later.&nbsp; Could you perhaps clarify a bit more, or give a specific example?<br>
<br>-Brent<br></div></div>