I am trying to understand how vector-set!
is implemented. It looks to me like vector-set!
is a special form - much like set!
is. When I look at examples using vector-set!
I see the following, desirable behavior (in guile).
(define test (lambda (v i) (vector-set! v i 0)))
(define v (make-vector 5 1))
v
$1 = #(1 1 1 1 1)
(test v 0)
v
$2 = #(0 1 1 1 1)
I can also do this (in guile)
(define test (lambda (v i) (vector-set! (eval v (interaction-environment)) i 0)))
(test (quote v) 3)
v
$21 = #(0 1 1 0 1)
Contrasting to the set!
behavior:
(define a 1)
(define test2 (lambda b (begin (set! b 0) b)))
(test2 (quote a))
$26 = 0
a
$27 = 1
In this case, to my understanding the set!
changes b
to 0 (and not the 'evaluated" b
(which should be a
). The eval
trick from above does not work here.
My question is: How is vector-set!
implemented compared to set!
(or set-variable-value!). Does vector-set!
peak at it's first argument? Or something else? I have tried to look at some of scheme implementations but extracting the gist from the code is tricky. Perhaps someone has an explanation or a link to some (sicp style) scheme implementation.
The function vector-set!
is a so-called primitive.
It is a function (not a special form), but it must be implemented within the runtime.
Note: A special form is a form that uses an evaluation order different from the order used in a normal application. Therefore if
, cond
, or
and others are special forms.
Some implementations (I can't remember if Guile is one of them) has a function primitive?
that can be used to test whether a function is a primitive or not.
> (primitive? vector-set!)
#t