Sunday, December 21, 2008

Tree Map Scheme

Very similar to the Map function for a list in scheme, you can apply a map to a tree.
This traverses the entire tree applying a map function to the value and stuffing back into a tree.
(define (tree-map f t)
(if (null? t)
(if (null? (cdr t))
(append (append (tree-map f (car t))
(list (f (cadr t))))
(tree-map f (caddr t))))

No comments: