;;;; Displaced adjustable arrays are one of the great mysteries. #| I wanted to convert a multidimensional array like #2A((1) (2)) to the same shape of list '((1) (2)) since common lisp's string formatter works with nested lists, for the go game I am playing with gef 7go board /users/screwtape/cgi-bin/go gopher.club 70 however I am not aware of a canonical way of doing this. My way is below. In particular I used a displaced array version of the multi-dim array, though I could have collected in a loop using #'ROW-MAJOR-AREF which ignores dimensions. A displaced array's elements are references to a different array. Here I am just making a flat array, but other things are possible. |# (setq *array* (make-array 6 :initial-element '0)) (setq *drray* (make-array 5 :adjustable t)) (adjust-array *drray* 3 :displaced-to *array*) (setf (aref *drray* 1) '1) (adjust-array *drray* 3 :displaced-to *array* :displaced-index-offset 3) (setf (aref *drray* 1) '2) (print *array*) (terpri) ;; #(0 1 0 0 2 0) (defun fold (list dims &aux (ims (cdr dims)) (mims (apply '* ims))) " fold (list list) (list dims) folds a flat list into dims. Must (= (length list) (apply '* dims)) " (cond (ims (loop for x below (truncate (length list) mims) collect (fold (loop for l in (subseq list (* x mims) (* (1+ x) mims)) collect l) (cdr dims)))) ((null ims) list))) (defun array2list (array &aux (ims (array-dimensions array)) (mims (apply '* ims))) " array2list ((array array)) displaces array to flat array to list then (fold list (array-dimensions array)) " (let ((flat-array (make-array mims :adjustable t))) (adjust-array flat-array mims :displaced-to array) (values (fold (loop for x across flat-array collect x) ims))))