How to add a character to the end of a line?

From my understanding, Strings are type vectors. While my attempts have been fruitless

(vector-push #\a "Hol") ;; "Hol" Is not of type vector and simple-array 

Is the literal "Hol" a constant vector in the same way that "(1 2 3) does not match (list 1 2 3)? Should I just create a character vector explicitly and add characters to it?

+4
source share
1 answer

You have the correct diagnosis. However, this is not even a question of literal data (for example, (list 1 2 3) vs. '(1 2 3) ) compared with the modified data. This means that the vector has a fill indicator . The documentation for vector-push and vector-push-extend says that the vector argument is a vector with a fill pointer. You will get a similar error with non-literal arrays that don't have a fill pointer, for example:

 (let ((v (make-array 3))) (vector-push nil v)) 

All you have to do is make sure that you create a vector with a fill pointer and large enough to hold what you click:

 (let ((v (make-array 2 :fill-pointer 0))) (print v) (vector-push 'xv) (print v) (vector-push 'yv) (print v) (vector-push 'zv) (print v)) 

vector-push does not configure the array, so you are not getting z in a vector from the final vector-push :

 #() #(X) #(XY) #(XY) 

If you make the vector adjustable and use vector-push-extend , you can get a larger array:

 (let ((v (make-array 2 :adjustable t :fill-pointer 0))) (print v) (vector-push 'xv) (print v) (vector-push 'yv) (print v) (vector-push-extend 'zv) (print v)) #() #(X) #(XY) #(XYZ) 

Using the type of the character element, you will do this with strings:

 (let ((v (make-array 2 :element-type 'character :adjustable t :fill-pointer 0))) (print v) (vector-push #\xv) (print v) (vector-push #\yv) (print v) (vector-push-extend #\zv) (print v)) "" "x" "xy" "xyz" 
+7
source

Source: https://habr.com/ru/post/1498047/


All Articles