我在方案中寫了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))
))))
有時我們覺得太辛苦了,不是嗎? –