我在 scheme 中编写了 90% 的树形图函数,但遇到了一个我遇到的主要问题。当我用二叉树测试我的代码时,除了第一个节点之外的所有节点都被正确映射。第一个节点退出,我似乎想不出办法解决这个问题。如有任何建议,将不胜感激。
(define (value tree)
(car tree))
(define (left tree)
(car (cdr tree)))
(define (right tree)
(car (cdr (cdr tree))))
(define (tree-map f T)
(cond ((null? T) '())
((and (null? (right T))(null? (left T))) '())
((and (null? (right T))(not (null? (left T))))
(make-tree (f (value(left T)))
(tree-map f (left T))
(right T)))
((and (null? (left T))(not (null? (right T))))
(make-tree (f (value(right T)))
(left T)
(tree-map f (right T))
))))