如何在常见的lisp中复制数组?

nny*_*thm 10 arrays functional-programming common-lisp

我想制作我的2D数组的副本,感觉就像处理数组的好的,功能性的,非破坏性的方式.这样做的方式是什么?

Sva*_*nte 15

UPDATE:现在,亚历山大有一个copy-array非常相似,下面给出的实施.用那个.

废弃的答案:我使用了以下内容,我认为它比当时的亚历山大版更好:

(defun copy-array (array &key
                   (element-type (array-element-type array))
                   (fill-pointer (and (array-has-fill-pointer-p array)
                                      (fill-pointer array)))
                   (adjustable (adjustable-array-p array)))
  "Returns an undisplaced copy of ARRAY, with same fill-pointer and
adjustability (if any) as the original, unless overridden by the keyword
arguments."
  (let* ((dimensions (array-dimensions array))
         (new-array (make-array dimensions
                                :element-type element-type
                                :adjustable adjustable
                                :fill-pointer fill-pointer)))
    (dotimes (i (array-total-size array))
      (setf (row-major-aref new-array i)
            (row-major-aref array i)))
    new-array))
Run Code Online (Sandbox Code Playgroud)

亚历山大版本的问题在于,adjust-array 黑客导致结果(至少在SBCL上)永远不会是一个 simple-array其他一些库(例如,视觉光学)所期望的.以上版本对我来说也更快.

其他人在不同的库中发布了一个非常相似的版本,但我忘记了人和库的名字.

  • 重新审视,我可以宣布,与此同时,亚历山大已经将其实施改为接近上述的内容.我现在建议使用亚历山大. (5认同)