sicp每日一题[2.46]
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:
(x_1, y_1) + (x_2, y_2) = (x_1+x_2, y_1+y_2),
(x_1, y_1) - (x_2, y_2) = (x_1-x_2, y_1-y_2),
s (x, y) = (sx, sy).
这道题没啥说的,差不多是最简单的题目了。
(define (make-vect x y)
(cons x y))
(define (xcor-vect vector)
(car vector))
(define (ycor-vect vector)
(cdr vector))
(define (add-vect v1 v2)
(make-vect (+ (xcor-vect v1)
(xcor-vect v2))
(+ (ycor-vect v1)
(ycor-vect v2))))
(define (sub-vect v1 v2)
(make-vect (- (xcor-vect v1)
(xcor-vect v2))
(- (ycor-vect v1)
(ycor-vect v2))))
(define (scale-vect s v)
(make-vect (* s (xcor-vect v))
(* s (ycor-vect v))))
(define v1 (make-vect 3 4))
(define v2 (make-vect -4 3))
(xcor-vect v1)
(ycor-vect v1)
(add-vect v1 v2)
(sub-vect v1 v2)
(scale-vect 0.2 (add-vect v1 v2))
; 执行结果
3
4
(mcons -1 7)
(mcons 7 1)
(mcons -0.2 1.4000000000000001)