在球拍的书中发现了这个功能:
(define (my-filter pred lst)
(cond [(empty? lst) empty]
[(pred (first lst))
(cons (first lst) (my-filter (rest lst)))]
[else (my-filter (rest lst))]))
用以下方式调用:
(my-filter (lambda (i) (> i 5)) `(1 2 3 4 5 6 7 8))
给出:
my-filter: arity mismatch;
the expected number of arguments does not match the given number
expected: 2
given: 1
arguments...:
'(2 3 4 5 6 7 8)
所以我改成了:
(define (my-filter pred lst)
(cond [(empty? lst) empty]
[(pred (first lst))
(cons (first lst) (my-filter pred (rest lst)))]
[else (my-filter pred (rest lst))]))
现在它工作正常。所以,如果我在那里错过任何东西,或者这本书是错的,我就会徘徊?
答案 0 :(得分:3)
第114页my-filter函数的定义有两个 参数,但重复时只提供一个参数。定义 应阅读:
(define (my-filter pred lst)
(cond [(empty? lst) empty]
[(pred (first lst))
(cons (first lst) (my-filter pred (rest lst)))]
[else (my-filter pred (rest lst)))))