我已经被这个问题困扰了一段时间了。我将一个谓词和一个列表传递给scheme中的另一个函数。如果谓词为true,则将其添加到答案列表中,否则跳过它。
例如,(myfilt positive? '(1 -2 3))应为(1 3)。但我一直在找(1 . 0)。
(define myfilt
(letrec ([testfilt (lambda (x poly function)
(if (empty? poly)
(function '())
(testfilt x (rest poly)
(lambda (v)
(function (if (x (car poly))
(cons (car poly) v)
0))))))]
[identity (lambda (x) x)])
(lambda (x poly)
(testfilt x poly identity))))发布于 2014-02-11 12:05:49
对于else分支,您最内部的if应该使用v,而不是0。因此:
(if (x (car poly))
(cons (car poly) v)
v)https://stackoverflow.com/questions/21692440
复制相似问题