# 练习 2.46¶

## 向量的表示¶

;;; 46-vect-represent.scm

(define (make-vect xcor ycor)
(list xcor ycor))

(define (xcor-vect v)
(car v))

(define (ycor-vect v)
(cadr v))


1 ]=> (load "46-vect-represent.scm")

;Loading "46-vect-represent.scm"... done
;Value: ycor-vect

1 ]=> (define v (make-vect 0.5 1))

;Value: v

1 ]=> (xcor-vect v)

;Value: .5

1 ]=> (ycor-vect v)

;Value: 1


## add-vect¶

;;; 46-add-vect.scm

(load "46-vect-represent.scm")

(define (add-vect vect another-vect)
(make-vect (+ (xcor-vect vect)
(xcor-vect another-vect))
(+ (ycor-vect vect)
(ycor-vect another-vect))))


1 ]=> (load "46-add-vect.scm")

;Loading "46-add-vect.scm"...
;  Loading "46-vect-represent.scm"... done
;... done
;Value: add-vect

1 ]=> (define sum (add-vect (make-vect 0.5 0.5)
(make-vect 0.2 0.2)))

;Value: sum

1 ]=> (xcor-vect sum)

;Value: .7

1 ]=> (ycor-vect sum)

;Value: .7


## sub-vect¶

;;; 46-sub-vect.scm

(load "46-vect-represent.scm")

(define (sub-vect vect another-vect)
(make-vect (- (xcor-vect vect)
(xcor-vect another-vect))
(- (ycor-vect vect)
(ycor-vect another-vect))))


1 ]=> (load "46-sub-vect.scm")

;Loading "46-sub-vect.scm"...
;  Loading "46-vect-represent.scm"... done
;... done
;Value: sub-vect

1 ]=> (define diff (sub-vect (make-vect 0.5 0.5)
(make-vect 0.2 0.2)))

;Value: diff

1 ]=> (xcor-vect diff)

;Value: .3

1 ]=> (ycor-vect diff)

;Value: .3


## scale-vect¶

;;; 46-scale-vect.scm

(load "46-vect-represent.scm")

(define (scale-vect factor vect)
(make-vect (* factor (xcor-vect vect))
(* factor (ycor-vect vect))))


1 ]=> (load "46-scale-vect.scm")

;Loading "46-scale-vect.scm"...
;  Loading "46-vect-represent.scm"... done
;... done
;Value: scale-vect

1 ]=> (define product (scale-vect 2 (make-vect 0.3 0.3)))

;Value: product

1 ]=> (xcor-vect product)

;Value: .6

1 ]=> (ycor-vect product)

;Value: .6


## 讨论 ¶

blog comments powered by Disqus