(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 ... :(
🔒 Locked
Scheme Programming
Advertisement
Original Post
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
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) ...
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) ...
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%)))
I'd imagine it'd look like:
(make-initialized-vector 3 (lambda (x) (make-object mass%)))
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 ?
(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 ?
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
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.
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.
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]
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.
Advertisement