;; Exercise 2.46. 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:
;;
(define (make-vect x y) (cons x y))
(define (xcor-vect vect) (car vect))
(define (ycor-vect vect) (cdr vect))
(define (add-vect vect1 vect2)
(let ((x1 (xcor-vect vect1)))
((x2 (xcor-vect vect2)))
((y1 (ycor-vect vect1)))
((y2 (ycor-vect vect2)))
(make-vect (+ x1 x2) (+ y1 y2))))
(define (sub-vect vect1 vect2)
(let ((x1 (xcor-vect vect1)))
((x2 (xcor-vect vect2)))
((y1 (ycor-vect vect1)))
((y2 (ycor-vect vect2)))
(make-vect (- x1 x2) (- y1 y2))))
(define (scale-vect vect s)
(let ((x (xcor-vect vect)))
((y (ycor-vect vect)))
(make-vect (* s x) (* s y))))
;;
(define (make-vect x y) (cons x y))
(define (xcor-vect vect) (car vect))
(define (ycor-vect vect) (cdr vect))
(define (add-vect vect1 vect2)
(let ((x1 (xcor-vect vect1)))
((x2 (xcor-vect vect2)))
((y1 (ycor-vect vect1)))
((y2 (ycor-vect vect2)))
(make-vect (+ x1 x2) (+ y1 y2))))
(define (sub-vect vect1 vect2)
(let ((x1 (xcor-vect vect1)))
((x2 (xcor-vect vect2)))
((y1 (ycor-vect vect1)))
((y2 (ycor-vect vect2)))
(make-vect (- x1 x2) (- y1 y2))))
(define (scale-vect vect s)
(let ((x (xcor-vect vect)))
((y (ycor-vect vect)))
(make-vect (* s x) (* s y))))
No comments:
Post a Comment