Representation of vectors

A two-dimensional vector v running from the origin to a point can be represented as a pair consisting of an x-coordinate and a y-coordinate. Implement a data abstraction for vectors by giving a constructor make-vect and corresponding selectors xcor-vect and ycor-vect. In terms of your selectors and constructor, implement procedures add-vect, sub-vect, and scale-vect 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) = (x1 − x2, y1 − y2)
           s·(x, y) = (sx, sy)

There are no comments yet.

Authentication required

You must log in to post a comment.

(define vec1 (make-vect 1.0 2.0))
(define vec2 (make-vect 0.5 0.5))

(check-equal? (xcor-vect vec1) 1.0)
(check-equal? (ycor-vect vec2) 0.5)
(check-equal? (add-vect vec1 vec2) (make-vect 1.5 2.5))
(check-equal? (sub-vect vec1 vec2) (make-vect 0.5 1.5))
(check-equal? (scale-vect 2 vec2) (make-vect 1.0 1.0))