arrays - 字符串和数组的集差

标签 arrays string lisp filtering common-lisp

set-difference 用作过滤函数,但仅适用于列表。什么是数组和字符串?这些类型的数据是否有类似的功能?如果没有这样的功能,实现它们的正确方法是什么?

现在我使用这个宏来处理任何序列作为列表(有时它很有用):

(defmacro treat-as-lists (vars &body body)
  (let ((type (gensym)))
    `(let ((,type (etypecase ,(car vars)
                    (string 'string)
                    (vector 'vector)
                    (list 'list)))
           ,@(mapcar (lambda (x) `(,x (coerce ,x 'list)))
                 vars))
       (coerce (progn ,@body) ,type))))

我的过滤器:

(defun filter (what where &key key (test #'eql))
  (treat-as-lists (what where)
    (set-difference where what :key key :test test)))

例子:

CL-USER> (filter "cat" "can you take this cat away?")
"n you ke his  wy?"
CL-USER> (filter #(0 1) #(1 5 0 1 9 8 3 0))
#(5 9 8 3)

最佳答案

由于编写适用于所有序列类型的函数通常意味着为列表和向量编写单独的版本,因此尽可能使用对序列进行操作的标准函数是值得的。在这种情况下,我们可以使用 positionremove-if .我颠倒了你的论点顺序,以便使这个序列差异更像集合差异,其中第二个参数从第一个参数中减去。

(defun sequence-difference (seq1 seq2 &key (start1 0) end1 (start2 0) end2
                                           key (key1 key) (key2 key)
                                           test test-not)
  "Returns a new sequence of the same type of seq1 that contains the
elements of the subsequence of seq1 designated by start1 and end1, and
in the same order, except for those that appear in the subsequence of
seq2 designated by start2 and end2. Test and test-not are used in the
usual way to elements produced by applying key1 (which defaults to
key) to elements from seq1 and by applying key2 (which defaults to
key) to elements from seq2."
  (flet ((in-seq2 (x)
           (not (null (position x seq2
                                :start start2 :end end2
                                :key key2
                                :test test :test-not test-not)))))
    (remove-if #'in-seq2 
               (subseq seq1 start1 end1)
               :key key1)))
(sequence-difference "can you take this cat away?" #(#\c #\a #\t))
;=> "n you ke his  wy?"

(sequence-difference "can you take this cat away?" #(#\c #\a #\t) :start1 3 :start2 1)
" you ke his c wy?"

请注意,该标准还包括 find ,适用于任意序列,但 find 返回“序列的一个元素,或 nil”。如果 nil 是序列的成员,这会导致歧义。另一方面,Position 返回一个索引(它将是一个数字,因此不是 nil)或 null,因此我们可以可靠地确定一个元素是否在序列中。

这里有一个重要的区别,您总是在这里得到一份副本。这样做的原因是主观的:由于序列函数通常采用开始和结束索引参数,因此最好在此处包含该功能。但是,如果我们要求 (sequence-difference "foobar""boa":start1 2) 那么我们想从“foobar”的子序列“obar”中删除字符 b、o 和 a ”。我们应该返回什么? “为了”还是“r”?也就是说,我们是否包括索引之外的 seq1 部分?在这个解决方案中,我决定不这样做,因此我正在执行 (remove-if … (subseq seq1 …) …),而 subseq 总是制作一个副本。另一方面,Set-difference 可能返回它的 list-1 或 list-2 参数,如果合适的话。这种实现通常不会返回 seq1 或 seq2,除非在某些异常情况下(例如,空列表)。

关于arrays - 字符串和数组的集差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24323753/

相关文章:

arrays - xPages 以作用域变量作为数据源重复控制

lisp - 彩票代码更正

python - 字符串中子串的最小范围

lisp - 关于SICP chpt 4.1 : How does (analyze expr) help speed up eval?的问题

lisp - 关于 LISP 程序,common lisp

php - 获取数组倒数第二个元素的键和值

arrays - 力扣 : Four Sum

javascript - 如何获取多维数组的索引?

Swift - 从字典中读取不同类型的值

c++ - 如何用整数解析字符串?