多次删除列表中的特定元素不区分大小写

D. *_*nga 0 lisp common-lisp

我一直在编写这段代码并研究递归运行函数的方法,并让它返回一个单词"the"被删除的列表.

我是Common Lisp的新手,我已经了解了基本功能,如setq, cons, cond, equal, carcdr.

当我运行代码时,我不断获取列表中的最后一个元素,如果有一个the后面的元素,则接下来.

谁能告诉我我做错了什么并引导我朝着正确的方向前进?

允许的Common Lisp构造是:COND,EQUAL(或EQUALP)CONS,CARCDR,以及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)

Rai*_*wig 5

该函数只是您的第一个函数,具有更好的命名:

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

  • @ triniplayaz1:你写了一个调用`my-remove`的函数(一个参数). (2认同)