問題2.46、問題2.47 – SICP(計算機プログラムの構造と解釈)その61

問題2.46

(define (make-vect x y)
  (cons x y))

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

(define (ycor-vect v)
  (cdr v))

(define (add-vect vect-a vect-b)
  (make-vect
    (+ (xcor-vect vect-a) (xcor-vect vect-b))
    (+ (ycor-vect vect-a) (ycor-vect vect-b))))

(define (sub-vect vect-a vect-b)
  (make-vect
    (- (xcor-vect vect-a) (xcor-vect vect-b))
    (- (ycor-vect vect-a) (ycor-vect vect-b))))

(define (scale-vect s v)
  (make-vect
    (* s (xcor-vect v))
    (* s (ycor-vect v))))

(define v1 (make-vect 1 2))
(define v2 (make-vect 3 5))
(add-vect v1 v2)
gosh> (4 . 7)
(sub-vect v1 v2)
gosh> (-2 . -3)
(scale-vect 2 v1)
gosh> (2 . 4)
(scale-vect 2 v2)
gosh> (6 . 10)

問題2.47

フレーム構成子 その1

(define (make-frame origin edge1 edge2)
  (list origin edge1 edge2))

(define (origin-frame frame)
  (car frame))

(define (edge1-frame frame)
  (cadr frame))

(define (edge2-frame frame)
  (caddr frame))

(define v0 (make-vect 1 2))
(define v1 (make-vect 2 4))
(define v2 (make-vect 4 8))
(define f (make-frame v0 v1 v2))
gosh> ((1 . 2) (2 . 4) (4 . 8))
(origin-frame f)
gosh> (1 . 2)
(edge1-frame f)
gosh> (2 . 4)
(edge2-frame f)
gosh> (4 . 8)

フレーム構成子 その2

(define (make-frame origin edge1 edge2)
  (cons origin (cons edge1 edge2)))

(define (origin-frame frame)
  (car frame))

(define (edge1-frame frame)
  (cadr frame))

(define (edge2-frame frame)
  (cddr frame))

(define v0 (make-vect 1 2))
(define v1 (make-vect 2 4))
(define v2 (make-vect 4 8))
(define f (make-frame v0 v1 v2))
gosh> ((1 . 2) (2 . 4) 4 . 8)
(origin-frame f)
gosh> (1 . 2)
(edge1-frame f)
gosh> (2 . 4)
(edge2-frame f)
gosh> (4 . 8)
計算機プログラムの構造と解釈
ジェラルド・ジェイ サスマン ジュリー サスマン ハロルド エイブルソン
ピアソンエデュケーション
売り上げランキング: 6542
«
»