我正在尝试编写一个函数,它接受一个列表,如果它包含重复的条目则返回 true,否则返回 false。我知道我应该使用成员。这是我到目前为止的尝试(失败了):
(defun dupl (lst)
(if (null lst) '())
(if ((member (car lst) (cdr lst)) (cons (car lst) (dupes (cdr lst))))
(t (dupl (cdr lst)))))
最佳答案
您的代码中存在一些问题。
- 第一个
if
应该使用return-from
来实际返回值。最好使用nil
而不是'()
。 - 在第二个
if
中,您尝试使用cond
语法。 - 我什至不确定您试图通过
cons
实现什么,但这似乎没有必要。
修复这些后,您的代码将如下所示:
(defun dupl (lst)
(if (null lst) (return-from dupl nil))
(if (member (car lst) (cdr lst))
t
(dupl (cdr lst))))
将两个 if
变成一个 cond
可能更简洁:
(defun dupl (lst)
(cond ((null lst) nil)
((member (car lst) (cdr lst)) t)
(t (dupl (cdr lst)))))
关于list - 检查列表是否包含重复项的谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35110201/