recursion - 递归函数不起作用 '"程序车中的参数类型错误”

标签 recursion scheme lisp cdr sisc

我正在编写一个递归函数,它接受一个元素 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) 因为 carcdr 不能在任何空列表上工作。

(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/

相关文章:

recursion - 有人可以解释递归在这些过程中是如何工作的吗

dll - 从 lisp 脚本、工作目录和路径加载库

emacs - 让 emacs 在启动时打开东西

Java:递归从未达到正确的条件

loops - 普通口齿不清 : recursive call from a loop

lisp - DrRacket 解释器是否使用基于 SICP 练习 1.5 的正常顺序评估?

lisp - 我怎样才能让 Lisp 编译器忽略一个 (label-variety) 函数?

algorithm - 树递归-打印给定数字的子序列

python - 递归、分治最大子数组

lisp - 从 .NET/C# 调用 LISP 或 SCHEME