Pregunta

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?

¿Fue útil?

Solución

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).

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top