Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate simple-arrays and keep the element-type '(unsigned-bytes 8) in Common Lisp?

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?

like image 803
Wise Simpson Avatar asked Dec 08 '22 07:12

Wise Simpson


2 Answers

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

like image 126
Frédéric Hamidi Avatar answered May 10 '23 22:05

Frédéric Hamidi


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.

like image 24
Samuel Edwin Ward Avatar answered May 10 '23 22:05

Samuel Edwin Ward