You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('') and can be up to 35 characters long.
37 lines
1.6 KiB
37 lines
1.6 KiB
(load "displaylib.scm")


(title "Exercise 2.46")


(doc "A twodimensional vector v running from the origin to a point can be represented as a pair consisting of an xcoordinate and a ycoordinate. Implement a data abstraction for vectors by giving a constructor makevect and corresponding selectors xcorvect and ycorvect. In terms of your selectors and constructor, implement procedures addvect, subvect, and scalevect that perform the operations vector addition, vector subtraction, and multiplying a vector by a scalar:




(x1,y1) + (x2,y2) = (x1+x2 , y1+y2)


(x1,y1)  (x2,y2) = (x1x2 , y1y2)


s . (x,y) = (sx, sy)


")




(define (makevect x y) (list x y))


(define (xcorvect v) (car v))


(define (ycorvect v) (cadr v))


(define (addvect v w) (makevect


(+ (xcorvect v) (xcorvect w))


(+ (ycorvect v) (ycorvect w))))


(define (subvect v w) (makevect


( (xcorvect v) (xcorvect w))


( (ycorvect v) (ycorvect w))))


(define (scalevect s v) (makevect


(* s (xcorvect v))


(* s (ycorvect v))))




(define origin (makevect 0 0))


(display origin)(newline)




(define oneone (makevect 1 1))


(define halfhalf (makevect 0.5 0.5))




(print "(addvect oneone halfhalf)")


(display (addvect oneone halfhalf))(newline)


(print "(subvect oneone halfhalf)")


(display (subvect oneone halfhalf))(newline)




(display "(scalevect .75 oneone)")(newline)


(display (scalevect .75 oneone))(newline)






