Skip to main content
gamedev.net
🔒 Locked

Scheme Programming

Started by DeVla 7 replies 650+ views
Original Post
DeVla
DeVla
Hello - The behavior of (make-vector k obj) is different than (vector ...) but (equal? (make-vector 3 0) (vector 0 0 0)) ==> #t Why ? (i can't understand) please help me. - How can i program (make-vector k obj) only using (vector ...) ? I tried (define (make-vector* k obj) (let ((temp '())) (do ((i 0 (+ i 1))) ((= i k) temp) (set! temp (cons obj temp))) (list->vector temp))) but it works like make-vector ... :(
SiCrane
SiCrane
I don't understand your question. (make-vector 3 0) creates a vector of 3 elements each initialied to 0, which is exactly what (vector 0 0 0) does. So of course (equal? (make-vector 3 0) (vector 0 0 0)) should be true.
DeVla
DeVla
Sorry my question was a little fuzzy :)
Maybe its completely the same with numbers or symbols but there is a little problem with objects ... :

I got a vector3d% class :

(define vector3d%
(class* object% ()

;-----------------Constructor--------------------------------

(init-field (x 0.0) (y 0.0) (z 0.0))

;-----------------Accessors----------------------------------

(define/public (get-x) (exact->inexact x)) ; Renvoi x
(define/public (get-y) (exact->inexact y)) ; Renvoi y
(define/public (get-z) (exact->inexact z)) ; Renvoi z

;-----------------To see-------------------------------------

(define/public (show) (list (get-x) (get-y) (get-z))) ;

;-----------------Mutators-----------------------------------

(define/public (set-x value) (set! x (exact->inexact value)))
(define/public (set-y value) (set! y (exact->inexact value)))
(define/public (set-z value) (set! z (exact->inexact value)))

(define/public (operator= v)
(begin
(set-x (send v get-x))
(set-y (send v get-y))
(set-z (send v get-z)))
this)

(super-instantiate ())))

I got a mass% class :

(define mass%
(class* object% ()

;-----------------Constructor--------------------------------

(init-field (pos (make-object vector3d%)) ;
(vel (make-object vector3d%)) ;
(force (make-object vector3d%)));

;-----------------Accessors---------------------------------

(define/public (get-pos) pos)
(define/public (get-vel) vel)
(define/public (get-force) force)

;-----------------Mutators------------------------------

(define/public (set-pos-x value) (send pos set-x value))
(define/public (set-pos p) (void (send pos operator= p)))
(define/public (set-vel v) (void (send vel operator= v)))
(define/public (set-force f) (void (send force operator= f)))

(super-instantiate ())))


So now i want a vector of masses :

(define vec1 (make-vector 3 (make-object mass%))

I want to set the x of the position of the first mass to 2 (for exmple)

I check if its init to 0.0 0.0 0.0 :

(send (send (vector-ref vec1 0) get-pos) show)

===> (0.0 0.0 0.0) o.k

(send (vector-ref vec1 0) set-pos-x 2)

I check :

(send (send (vector-ref vec1 0) get-pos) show)

===> (2.0 0.0 0.0) o.k

(send (send (vector-ref vec1 1) get-pos) show)

===> (2.0 0.0 0.0) W-T-F ??????????

------------------------------------------------

BUT if i a create my vector with (vector ...) like

(define vec2 (vector (make-object mass%)
(make-object mass%)
(make-object mass%)))

(send (vector-ref vec2 0) set-pos-x 2)

(send (send (vector-ref vec2 0) get-pos) show)

===> (2.0 0.0 0.0) o.k

(send (send (vector-ref vec2 1) get-pos) show)

===> (0.0 0.0 0.0) O.K

So there is something not very clear (for me) with (make-vector k obj) ...





SiCrane
SiCrane
The problem is that when you use make-vector you're binding the same instance to all three cells of the vector. You might try to make-initialized-vector instead. Since make-initialized-vector stores the result of a function evaluation to each cell instead of the same element every time, it should give you different objects this time.

I'd imagine it'd look like:
(make-initialized-vector 3 (lambda (x) (make-object mass%)))
DeVla
DeVla
Big thanks SiCrane ... you are THE BEST ! :)

(build-vector n f) is the easiest way create large vectors of unique masses ... big big big thnx !

A question from a noob :) , is the function (vector-ref v k) returns a sort of pointer ? I mean, by this function i catch the cell of index k and can directly change it, or its something more complex ? :)

My goal was to create a table of pointers (like in C++), (i know that Scheme is full of pointers) do you think this vector could approach this ?

Don't you think that hash-tables would be better for this ?




SiCrane
SiCrane
vector-ref returns the element of the vector by value. If you store the result of a vector-ref call, modification of the original vector won't be reflected in the the stored value.

Ex: if you do
(define a (vector 0 0 0))(define b (vector-ref a 0))(vector-set! a 0 5)

b will still have the value 0 not 5.

I'm not entirely sure what you're trying to do, so I can't really comment on what's best for you.
DeVla
DeVla
How can i avoid "call by value"? is the form (define-syntax ...) the only way to do it ? (i mean to have direct acces to the arguments)
SiCrane
SiCrane
The problem isn't call by value, its the return value that's giving you troubles. What you can do is bind the elements of a vector to cells and then alias the cells. However, trying to bind references like this isn't very scheme-like.
DeVla
DeVla
I'm trying to write the lesson 40 in Scheme (i finished it) but
unfortunatly i got some real performance troubles ... maybe the major reason comes from the class field accessing-mutating using the send form. I (suppose) i've done some optimisations and there is no significant effect ... :(

I think posting my code here is futile (maybe i'm wrong) because its too big.

SiCrane (or all peeps who want to help me in this project) if you post your mail i'll send you all my code so maybe you could give me any advice.

Thanks.

[Edited by - DeVla on June 27, 2004 10:37:28 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.