列出方案中树的偶数值

时间:2014-06-02 05:38:54

标签: scheme racket

编写一个程序,返回树的偶数列表

我这样做:

(define (list_pares arbol)
  (cond
    [(empty? arbol) 0]
    [(and (es-hoja? arbol) (even? (dato-tree arbol)))
     (list (dato-tree arbol))]
    [else 
     (cond
       [(even? (dato-tree arbol))
        (append (list (dato-tree arbol))
                (list_pares (left-tree arbol))
                (list_pares (right-tree arbol)))]
       [else
        (append
         (list_pares (left-tree arbol))
         (list_pares (right-tree arbol)))])]))

但是当跑步时:

(list_pares (list 2 empty (list 5 (list 4 empty empty) (list 9 (list 6 empty empty) empty))))

给我这个错误:

append: last argument must be a list, but received 0

怎么可能?

1 个答案:

答案 0 :(得分:0)

您的错误消息来自:

 [(empty? arbol) 0]

因此,当您追加结果的结果以0而不是空列表结束时,您会遇到此问题,因为不允许使用(append '(something) 0 '(something else))

如果给你一棵空树并且应该从中返回一个偶数值列表,你应该真正返回什么?