Common Lisp: how to access a string of a specific multidimensional array?

Say I wrote

(setf s (make-array (list 9 9) :element-type 'bit)) 

so s is a 9x9 bit matrix.

and I want to get the 1st row s. How to get it?

I could do the following:

 (setf s (make-array 9 :element-type 'array :initial-element (make-array 9 :element-type 'bit))) 

and access the first line (svref s 0).
But I want to know if there is a built-in way.
(And the 2-dimensional array seems to allocate fewer bytes).

+4
source share
1 answer
 (defun array-slice (arr row) (make-array (array-dimension arr 1) :displaced-to arr :displaced-index-offset (* row (array-dimension arr 1)))) 

This only works for line slices, not IIRC, copy the array. Writing to a slice will change the original array.

+4
source

Source: https://habr.com/ru/post/1433082/


All Articles