我正在尝试解决一个难题,即爬取二叉树以查找特定值是否作为节点存在。我在评估一对看起来像'(1 '())
. 我认为当我评估(= 4 '())
它时返回true,这显然是不正确的。
我尝试删除cons
添加空对的,但是我现在得到以下内容:
(#f . #f)
我相信它不是一对。我对如何通过cons
.
我的代码如下:
我的自制any?
功能
(define (any? f lst) (not (null? (filter f lst))))
带有 with 的cons
版本'()
:
(define value-exist?
(lambda (n xs)
(if (null? xs)
'()
(letrec ((node-left (car xs))
(node-right (cdr xs))
(true? (lambda (x) x)))
(if (list? node-left)
(any? true? (cons (value-exist? n node-left)
(cons (value-exist? n node-right)
'())))
(any? true? (cons (= n node-left)
(cons (value-exist? n node-right)
'()))))))))
我删除cons
with的版本'()
:
(define value-exist?
(lambda (n xs)
(if (null? xs)
'()
(letrec ((node-left (car xs))
(node-right (cdr xs))
(true? (lambda (x) x)))
(if (list? node-left)
(any? true? (cons (value-exist? n node-left)
(value-exist? n node-right)))
(any? true? (cons (= n node-left)
(value-exist? n node-right)))
)))))
示例调用:
(value-exist? 4 '(1 2 3 (3 2 3)))