我一直在编写这段代码并研究递归运行函数的方法,并让它返回一个单词"the"被删除的列表.
我是Common Lisp的新手,我已经了解了基本功能,如setq, cons, cond, equal, car
和cdr
.
当我运行代码时,我不断获取列表中的最后一个元素,如果有一个the
后面的元素,则接下来.
谁能告诉我我做错了什么并引导我朝着正确的方向前进?
允许的Common Lisp构造是:COND
,EQUAL
(或EQUALP
)CONS
,CAR
和CDR
,以及Common Lisp的一些基本原始构建块.
我不能使用任何预定义的函数来实际消除.
它应该是这样的样子.样品运行:
(filter-out-the '(There are the boy and THE girl and The Rose))
Run Code Online (Sandbox Code Playgroud)
返回:
(THERE ARE BOY AND GIRL AND ROSE)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
(defun list_member (x L)
(cond ((null L) nil)
((equal x (car L))
(list_member x (cdr L)))
(T (cons (car l) (list_member x (cdr L))))))
(defun filter-out-the (L)
(setq x '(the))
(cond ((null L) nil)
((list_member (car x) (cdr L )) (filter-out-the (cdr L)))
(T (cons (car L) (filter-out-the (cdr L))))))
Run Code Online (Sandbox Code Playgroud)
该函数只是您的第一个函数,具有更好的命名:
(defun my-remove (item list)
(cond ((null list) nil)
((equal item (first list))
(my-remove item (rest list)))
(T (cons (first list)
(my-remove item (rest list))))))
Run Code Online (Sandbox Code Playgroud)
你可以称之为:
CL-USER 36 > (my-remove 'the '(there are the boy and the girl and the rose))
(THERE ARE BOY AND GIRL AND ROSE)
Run Code Online (Sandbox Code Playgroud)