Created
August 24, 2010 10:39
-
-
Save emasaka/547348 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (traverse-tree tree func) | |
(define (spin cns prev) | |
(let ((next (car cns))) | |
(set-car! cns (cdr cns)) | |
(set-cdr! cns prev) | |
next )) | |
(let ((marker (gensym))) | |
(let loop ((cns tree) (prev marker)) | |
(let ((next (spin cns prev))) | |
(cond ((pair? next) | |
(loop next cns) ) | |
((not (eq? next marker)) | |
(or (null? next) (func next)) | |
(loop cns next) ))))) ) | |
(define sample-list '((3 (5 (7)) 11))) | |
(let ((tree (list-copy sample-list))) | |
(traverse-tree tree print) | |
(print tree) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment