非破坏性子序列-除了Lisp

时间:2019-04-07 04:31:36

标签: lisp common-lisp subsequence

我知道Lisp中的subseq通过范围返回一个子序列。除了该范围,还有什么东西可以返回子序列,并且应该是非破坏性的吗?任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

您可以使用concatenatesubseq对任何序列进行此操作:

(defun sequence-except (sequence start end)
  (concatenate (sequence-type sequence)
               (subseq sequence 0 start)
               (subseq sequence end)))

以下内容足以确定位向量,字符串等的输入序列类型:

(defun sequence-type (sequence)
  (etypecase sequence
    (list 'list)
    (array `(array ,(array-element-type sequence) (*)))))

测试:

(loop for test in (list
                   #*10101001
                   "abcd"
                   '(0 3 2)
                   nil
                   #(1 2 3 4)
                   (make-array 4
                               :adjustable t
                               :fill-pointer T
                               :initial-contents
                               '(a b c d)))
      collect (concatenate (sequence-type test) test))

答案 1 :(得分:0)

你不能只是:

(defun seq-drop-subseq (SEQ START &optional END)
  (if END
      (nconc (seq-take START) (seq-drop END))
    (seq-take START)))

编辑:不得不去看看。 remove做到了。