非破坏性子序列——lisp 除外
Non destructive subsequence-except in lisp
我知道 lisp 中的 subseq return 一个范围内的子序列。有什么东西可以 return 除了那个范围之外的子序列并且应该是非破坏性的吗?感谢您的帮助。
你就不能:
(defun seq-drop-subseq (SEQ START &optional END)
(if END
(nconc (seq-take START) (seq-drop END))
(seq-take START)))
编辑:必须去看看。 remove
做到了。
您可以使用 concatenate
和 subseq
对任何序列执行此操作:
(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))
我知道 lisp 中的 subseq return 一个范围内的子序列。有什么东西可以 return 除了那个范围之外的子序列并且应该是非破坏性的吗?感谢您的帮助。
你就不能:
(defun seq-drop-subseq (SEQ START &optional END)
(if END
(nconc (seq-take START) (seq-drop END))
(seq-take START)))
编辑:必须去看看。 remove
做到了。
您可以使用 concatenate
和 subseq
对任何序列执行此操作:
(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))