可扩展的矢量类型

dav*_*ugh 3 vector common-lisp adjustable-array

如何正确指定可扩展向量的common-lisp类型(即,vector-push-extend可接受),因此可以复制它.例如,如果向量定义为:

(defparameter v (make-array 2
                            :initial-contents '((a (b)) (c (d) e))
                            :adjustable t
                            :fill-pointer t))
Run Code Online (Sandbox Code Playgroud)

我复制它的简单(不正确)方法是:

(map 'array #'copy-tree v)
Run Code Online (Sandbox Code Playgroud)

但这会在sbcl中生成一个类型错误.一个正确的序列类型规范可以使这个工作吗?

cor*_*ump 5

简单的副本

你可以这样做:

(map (type-of v) #'copy-tree v)
Run Code Online (Sandbox Code Playgroud)

什么类型?

CL-USER> (type-of v)
(VECTOR T 2)
Run Code Online (Sandbox Code Playgroud)

以下就足够了:

(map 'vector #'copy-tree v)
Run Code Online (Sandbox Code Playgroud)

此图有助于记住类型层次结构,尤其是数组和向量.

可调节,填充指针,......

但是,得到的矢量不可调整.也许这样的事情可以帮助:

(defun my-copy (vector)
  (map-into (make-array (array-dimensions vector)
                        :adjustable (adjustable-array-p vector)
                        :fill-pointer (fill-pointer vector))
            #'copy-tree
            vector))
Run Code Online (Sandbox Code Playgroud)