LISP-每次出现搜索原子后的原子
我试图在列表中的搜索原子之后添加一个新原子 当我尝试递归调用函数appendConst时,我得到以下错误-LISP-每次出现搜索原子后的原子,lisp,elisp,Lisp,Elisp,我试图在列表中的搜索原子之后添加一个新原子 当我尝试递归调用函数appendConst时,我得到以下错误- [1]> (defun appendConst (OLD NEW L) (cond ((null L) ()) ((EQ (car L) OLD) (appendConst (cons OLD (cons NEW (cdr L)))) ) (T (cons (car L) (appendConst OLD NEW
[1]> (defun appendConst (OLD NEW L)
(cond
((null L) ())
((EQ (car L) OLD) (appendConst (cons OLD (cons NEW (cdr L)))) )
(T (cons (car L) (appendConst OLD NEW (cdr L))))
))
APPENDCONST
[2]> (appendConst 'a 'd '(a c d e a m k))
*** - EVAL/APPLY: Too few arguments (1 instead of at least 3) given to APPENDCONST
The following restarts are available:
ABORT :R1 Abort main loop
Break 1 [3]>
在这里,使用我的输入,在每次出现“a”之后,我想在给定的整个列表中添加一个“d”
我是新手,这里如何递归调用函数
TIA.您的第一个递归调用(在其中添加新的实际上只有一个参数,错误表明它有。仅仅添加缺少的OLD
和NEW
将不起作用,因为递归调用将再次找到OLD
。因此,只需在通话之外进行思考,就像在其他情况下一样:
(cons OLD (cons NEW (appendConst OLD NEW (cdr L))))
请注意,这两种情况都不是尾部递归:为了避免堆栈的使用与列表的长度成比例,您必须就地修改列表,或者执行更复杂的操作,例如在递归时传递新列表的头和尾