lisp - 测试列表的所有元素是否彼此不同

标签 lisp common-lisp

我有一个列表列表,想测试所有元素是否彼此不同,即 equal 应该为列表元素的所有组合返回 nil。

例如

(defparameter feld '((1 0 0 5 5 0) 
                     (0 0 0 0 0 0) 
                     (1 1 5 5 0 0) 
                     (0 1 0 1 5 5) 
                     (5 5 1 0 1 0) 
                     (1 0 1 0 5 5)))

我想过使用 reduce 但据我所知它只测试邻居的相等性,就像循环构造一样:

(loop for i below (length feld) 
      for j from 1 
          if (equal (nth i feld) (nth j feld)) return t)

是否有一种使用标准构造的简单方法,我目前没有看到,或者我是否必须创建递归函数?

整个数据结构代表一个“棋盘游戏”,其中每个列表都是棋盘上的一行,内部列表中的每个元素都是这个字段的值。三个数值(0、1 和 5)类似于空、符号 A 和符号 B。有效的板不能有两条相同的线。这就是为什么我想识别它们。

基本上,它就像删除重复项而不删除。与此同时,我在想这样的事情:

(defun duplicates-p (lst)
  (cond ((null lst) '())
        ((member (car lst) (cdr lst)) t)
        (t (duplicates-p (rest lst)))))

最佳答案

像这样:

(defun unique (lsts &aux (h (make-hash-table :test 'equal)))
  (loop :for lst :in lsts
        :never (gethash lst h)
        :do (setf (gethash lst h) t)))

关于lisp - 测试列表的所有元素是否彼此不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40565458/

相关文章:

syntax - 本地过程绑定(bind)

recursion - 计算在 Towers of hanoi Lisp 中移动的磁盘数量

lisp - 包 EXT 不存在

namespaces - 同时绑定(bind)值和函数的符号有什么优势?

xpath - LISP Xpath 按顺序获取多个节点

common-lisp - dotimes中floor的多个返回值

lisp - 如何在 defun 中 defun 一个函数?

common-lisp - 为什么 (equal (copy-<some struct type> <struct type object>) <same struct type object>) nil?

common-lisp - `eval-when` 的意外行为

scheme - #f 在 repl.it 中输出方案结果