Adam Warner <usenet@consulting.net.nz> wrote:
+---------------
| Hi Rob Warnock,
| > Well, what about this, then?
| > > (make-array 10 :element-type 'double-float
| > :initial-contents '(0d0 1d0 2d0 3d0 4d0 5d0 6d0 7d0 8d0 9d0))
| > #(0.0d0 1.0d0 2.0d0 3.0d0 4.0d0 5.0d0 6.0d0 7.0d0 8.0d0 9.0d0)
| > > (type-of *)
| > (SIMPLE-ARRAY DOUBLE-FLOAT (10))
|
| What's your point Rob? (Mine was that the semantics of readable printed
| objects broke because Common Lisp doesn't print and read back in the type
| of the array along with the array contents)
+---------------
And mine was simply that there is an obvious workaround for the problem
you presented [reading specialized vectors] that is less painful than
looping over a setf/aref, namely, passing the vector you got from READ
to MAKE-ARRAY's :INITIAL-CONTENTS.
Though I apologize for not making it clear in my example that it can be
made fully automatic, provided that you *know* the array you've just read
is homogenous:
- The dimensions can be extracted from the read-in array;
- The :ELEMENT-TYPE can be extracted from the read-in array;
- :INITIAL-CONTENTS can take a vector, not just a list.
But you already knew all that, yes?
> (defun coerce-to-specialized-array (x)
(make-array (array-dimensions x)
:element-type (type-of (row-major-aref x 0))
:initial-contents x))
COERCE-TO-SPECIALIZED-ARRAY
> (read-from-string "#(0d0 1d0 2d0 3d0 4d0 5d0 6d0 7d0 8d0 9d0)")
#(0.0d0 1.0d0 2.0d0 3.0d0 4.0d0 5.0d0 6.0d0 7.0d0 8.0d0 9.0d0)
42
> (type-of *)
(SIMPLE-VECTOR 10)
> (coerce-to-specialized-array **)
#(0.0d0 1.0d0 2.0d0 3.0d0 4.0d0 5.0d0 6.0d0 7.0d0 8.0d0 9.0d0)
> (type-of *)
(SIMPLE-ARRAY DOUBLE-FLOAT (10))
>
Note: The above COERCE-TO-SPECIALIZED-ARRAY has a problem for multi-
dimensional arrays, since the latter are not "sequences" [vectors &
lists only], and hence are not valid arguments for :INITIAL-CONTENTS.
Is there a standard idiom to fix this?
-Rob
-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607