I got two simple-arrays, of which element-type is '(unsigned-byte 8). If I use (concatenate 'vector array-A array-B)
, the result array's element-type won't be '(unsigned-byte 8) anymore.
How can I concatenate arrays and keep their element-type in Lisp?
As Rainer Joswig points out in the comments, you could simply specify the element type of your result vector:
(concatenate '(vector (unsigned-byte 8)) array-A array-B)
Note that the documentation says:
If the result-type is a subtype of vector, then if the implementation can determine the element type specified for the result-type, the element type of the resulting array is the result of upgrading that element type.
Therefore, your LISP implementation is free to choose a "better" element type for the result vector (this might happen if you specify something like (unsigned-byte 5)
, for instance).
You might try doing something like this to return an array of the same type as the first array:
(concatenate (type-of array-A) array-A array-B)
Unfortunately, this won't work very well because the type may include the length of the array, and you probably want a longer array.
If you're willing to lose a little generality, you can go with:
(concatenate `(vector ,(array-element-type array-A)) array-A array-B)
This will return a vector with the same element type as array-A
, whatever that may be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With