Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutate vector in scheme by appending vectors

I don't know the underlying implementation of vectors in Scheme, hence not sure how to write a vector-append!

Prototype:

(define (vector-append! vect . vects)
  ; definition here 
  )

P.S. Preferred to use vector over list since vector-ref is a constant time operation [src]

like image 709
ajmartin Avatar asked Mar 22 '23 13:03

ajmartin


2 Answers

You can't resize a vector after its creation, so vector-append! can not be implemented as an in-place operation. What you can do, is create a new vector with size equal to the sum of all the subvectors' sizes, where the elements in all subvectors will be copied one after the other.

Use vector-grow as the starting procedure, and work from there. You'll have to play a bit with indexes to get a working solution.

like image 155
Óscar López Avatar answered Mar 27 '23 12:03

Óscar López


Vectors are not resizeable. So a vector-append! that extends a vector's size is not possible.

like image 31
Chris Jester-Young Avatar answered Mar 27 '23 13:03

Chris Jester-Young