gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:56:39 25 4
gpt4 key购买 nike

我一直在编写这段代码并研究递归地运行一个函数并让它返回一个列表,其中删除了单词“the”。

我是 Common Lisp 的新手,我已经了解了 setq、cons、cond、equal、carcdr 等基本函数。

当我运行代码时,我一直在获取列表中的最后一个元素,如果它后面有 the,它就会跟随。

任何人都可以告诉我我做错了什么并引导我朝着正确的方向前进吗?

允许的 Common Lisp 结构是:CONDEQUAL(或 EQUALP)、CONSCARCDR,以及 Common Lisp 的一些基本原始构建 block 。

我不能使用任何预定义函数来进行实际消除。

应该是这样的示例运行:

(filter-out-the   '(There are the boy and THE girl and The Rose))

返回:

(THERE ARE BOY AND GIRL AND ROSE)

这是我的代码:

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

最佳答案

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

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

你可以直接调用它:

CL-USER 36 > (my-remove 'the '(there are the boy and the girl and the rose))
(THERE ARE BOY AND GIRL AND ROSE)

关于lisp - 多次删除列表中的特定元素不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27299633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com