Mutant vector in the scheme by adding vectors

I don’t know the basic implementation of vectors in Scheme, so I don’t know how to write vector-append!

Prototype:

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

PS It is preferable to use a vector list, since vector-ref is a constant-time operation [src]

+4
source share
4 answers

You cannot resize a vector after creating it, therefore vector-append! cannot be implemented as an on-site operation. What you can do is create a new vector with a size equal to the sum of all sizes of the subvectors, where the elements in all the subvectors will be copied one by one.

Use vector-grow as an initial procedure and work from there. You will need to play a little with the indexes to get a working solution.

+5
source

Vectors are not resized. So vector-append! A that extends a vector size is not possible.

+2
source

Here is my implementation. Sorry, comments are from now on, not when I wrote this function.

Also, do not think that you can mutate a vector directly, since they are fixed.

 (define (vector-append . rest) (letrec ((all-vectors? (lambda (vector . rest) (and (vector? vector) (or (null? rest) (apply all-vectors? rest))))) (++ (lambda (x) (+ x 1))) (gobble-next (lambda (big-i small-i vec-big vec-small) (if (= small-i (vector-length vec-small)) 'gobbled (begin (vector-set! vec-big big-i (vector-ref vec-small small-i)) (gobble-next (++ big-i) (++ small-i) vec-big vec-small))))) (helper (lambda (i vector . rest) ;;the i here keeps track of the place in the new-vec (if (null? rest) ;;to start copying at (begin (gobble-next i 0 new-vec vector) new-vec) (begin (gobble-next i 0 new-vec vector) (apply helper (cons (+ i (vector-length vector)) rest)))))) (new-vec (make-vector (apply length-of-vectors rest)))) ;;end of letrec (cond ((null? rest) (error "error, no arguments to vector-append")) ((not (apply all-vectors? rest)) (error "arguments not all vectors")) ((null? (cdr rest)) (car rest)) (else (apply helper (cons 0 rest)))))) (define length-of-vectors (lambda (vector . rest) (+ (vector-length vector) (if (null? rest) 0 (apply length-of-vectors rest))))) 
0
source

Usually '!' implies a destructive operation on the Scheme. You have not made it clear that you want a destructive implementation. I will give both of you, it seems. First a non-destructive option, simply:

 (define (vector-append! vect . vects) (list->vector (apply append (map vector->list (cons vect vects))))) 

Now for the mutable, you will need your own vector abstraction, because the Scheme vectors are immutable in length. Do not let this distract you; if you need it, you need it.

 (define (my-vector . values) (cons 'MY-VECTOR (list->vector values))) (define my-vector-values cdr) ; private (define (my-vector-ref vect index) (vector-ref (my-vector-values vect) index)) (define (my-vector-append! vect . vects) (set-cdr! vect (apply vector-append! (map my-vector-values (cons vect vects))))) 

and then some helpers for your new abstraction:

 (define (list->my-vector list) (cons 'MY-VECTOR (list->vector list))) (define (vector->my-vector vector) (cons 'MY-VECTOR vector) ; maybe ;; etc 
0
source

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


All Articles