我正在使用Common lisp编程,我需要一个能够删除(至少)两个公共元素的子列表的算法。
我不知道如何解决问题。我想用这个:
(defun remove-duplicates-list(list)(删除重复列表:test' equal:key' cdr))
但是子列表之间的cdr是不同的,我只是无法弄清楚我如何计算两个元素并删除子列表。 此外,我需要删除"原始"子列表(在示例中为#1;(1 2 3 4)),仅限机器人(2 3 4)和(1 3 4)
示例:
输入:'((1 2 3 4)(2 3 4)(5 6 7 8)(1 3 4)(9 9 9 9))
输出:'((1 2 3 4)(5 6 7 8)(9 9 9 9))
输入:'(((1.1)(2.2)(3.3)(4.4))((1.1)(2.2)(4.4)))
输出:'((1.1)(2.2)(3.3)(4.4))
谢谢!
抱歉,如果,起初,我没有很好地解释我的问题,我在帖子中纠正了很多错误。这是我对这个社区的第一个问题,请原谅
答案 0 :(得分:1)
Actually you could do it with remove-duplicates
. You need to make a test function that returns true when at least two elements are similar. eg.
(defun two-similar-p (lst1 lst2)
...)
(two-similar-p '(1 2 3) '(1 4 5)) ; ==> nil
(two-similar-p '(1 2 5) '(1 4 5)) ; ==> t
Using a hash is the fastest and best time complexity while iterating one list (length other-list)
times is possibly the easiest. Then you can solve your problem this way:
(defun remove-duplicates-list (list)
(remove-duplicates list :test #'two-similar-p :from-end t))
(remove-duplicates-list '((1 2 3 4) (2 3 4) (5 6 7 8) (1 3 4) (9 9 9 9)))
; ==> ((1 2 3 4) (5 6 7 8) (9 9 9 9))