Skip to content

Instantly share code, notes, and snippets.

@emasaka
Created August 24, 2010 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emasaka/547348 to your computer and use it in GitHub Desktop.
Save emasaka/547348 to your computer and use it in GitHub Desktop.
(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