<br><div><span class="gmail_quote">On 10/20/07, <b class="gmail_sendername">Andrew Coppin</b> <<a href="mailto:andrewcoppin@btinternet.com">andrewcoppin@btinternet.com</a>> 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'm writing some code where I take an expression tree and transform it<br>into another equivilent one.<br><br>Now it'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> transform :: Expression -> [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'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'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> transform (...sub1...sub2...) =<br> let<br> sub1s = transform sub1<br> sub2s = transform sub2<br> in map (\sub1' -> put sub1' back into main expression) sub1s ++ map
<br>(\sub2' -> put sub2' back into main expression) sub2s<br><br>After you've typed that a few times, it becomes *very* boring! But I<br>can't think of a clean way to abstract it. :-(<br><br>It's *almost* like you want to use the list monad:
<br><br> transform (...sub1...sub2...) = do<br> sub1' <- transform sub1<br> sub2' <- transform sub2<br> return (put sub1' and sub2' back into the main expression)<br><br>Except that that doesn'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'm having trouble understanding exactly what you want. In particular, I don't understand what this statement:
<br><br>"But what I *really* want is to print out the transformation *sequence*."<br><br>has to do with the pseudocode that you exhibit later. Could you perhaps clarify a bit more, or give a specific example?<br>
<br>-Brent<br></div></div>