我正在编写一个递归函数,它接受一个元素 A 和一个列表 L,并返回一个等于 L 的列表,但每次出现的 A 都会被删除。这是我写的:
(define (remove A L)
(cond ( (eq? A (car L)) (remove A (cdr L)) )
( (not(eq? A (car L))) (cons (car L) (remove A (cdr L))) )
( (null? L) '() )
)
)
编译运行时出现如下错误:
/tmp/compile/5c6515d8-e155-11e5-9605-aa00009baa05/input/main.scheme:2:21: In procedure remove:
/tmp/compile/5c6515d8-e155-11e5-9605-aa00009baa05/input/main.scheme:2:21: In procedure car: Wrong type argument in position 1 (expecting pair): ()
最佳答案
我想通了:
函数的第一次检查需要是 (null?L)
因为 car
和 cdr
不能在任何空列表上工作。
(define (remove A L)
(cond ( (null? L) '() )
( (equal? A (car L)) (remove A (cdr L)) )
( (not(equal? A (car L))) (cons (car L) (remove A (cdr L))) )
)
)
关于recursion - 递归函数不起作用 '"程序车中的参数类型错误”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35775319/