我更新了我的代码,尽管我还没有找到解决方案,但是我到达了。这是一项家庭作业,所以我不是在找人为我解决问题。我想自己解决这个问题。只是想展示我的进步。
(define has-subtarget?(lambda(ls thirdVal sumA sumB)
(cond
((and (null? (cdr ls)) (= sumA sumB) (= sumA sumB) (= sumA thirdVal) (> sumA 0) (> sumB 0)))
(null? (cdr ls))
(else
(has-subtarget?(cdr ls) thirdVal (+ (car ls) sumA) sumB)
(has-subtarget?(cdr ls) thirdVal sumA (+ (car ls) sumB))
(if (= thirdVal 0)
(has-subtarget?(cdr ls) (car ls) sumA sumB)
))
)
#f
))
下面是我的原始代码给我一个错误:
"+: contract violation expected: number? given: last argument position: 1st"
我发现由于某种原因,它是将我的(+ addToA sumA)读取为列表,而不是要从加法返回的值,以使我的递归程序正常工作。
这是我调用函数的方式:
(has-subtarget? 0 0 0 0 0 '(1 2 3 4))
(define has-subtarget?
(lambda (addToA addToB thirdSetVal sumA sumB lst)
(cond
((and (and (and (and (null? (cdr'(lst))) (= sumA sumB)) (> 0 sumA)) (> 0 sumB)) (= sumA thirdSetVal)) #t)
(else
(has-subtarget? 0 (car'(lst)) thirdSetVal (+ addToA sumA) (+ addToB sumB) (cdr'(lst)))
(has-subtarget? (car'(lst)) 0 thirdSetVal (+ addToA sumA) (+ addToB sumB) (cdr'(lst)))
(if((or(and (= sumA 0) (= sumB 0)) (= thirdSetVal 0)))
(has-subtarget? 0 0 (car'(lst)) (+addToA sumA) (+ addToB sumB) (cdr'(lst))))
)))
)