<br><br><div><span class="gmail_quote">On 7/30/07, <b class="gmail_sendername">Zeng Fucen</b> <<a href="mailto:zengfucen@gmail.com">zengfucen@gmail.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 a scheme novice,<br><br>I want to travel a binary tree, how to ? (depth first)<br><br>Thanks for your tips , first!<br><br>I define a tree like this: (1 (2 4 5) (3 6 7))<br><br>_________________________________________________
<br> For list-related administrative tasks:<br> <a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br></blockquote></div><br>The syntax for defining a list of any sort is either:
<br>(list ...)<br>'(...) <- What's inside will NOT be evaluated.<br><br>Hans gave a nice example:<br><br>"(1 (2 (4 () ()) (5 () ())) (3 (6 () ()) (7 () ()))"<br><br>You don't *need* it, but it makes more sense that way.
<br><br>(define (travel-tree l)<br> (cond ((null? l) ())<br> ((list? l) (travel-tree (car l)))<br> (t (travel-tree (cdr l))))<br><br>The above method _should_ work, but I can't guarantee it because I have my own problems.
<br>I assume that it'll be tail-recursive, considering that it will not evaluate the rest of the condition if it succeeds.<br><br>All in all, I hope this helps.<br>