sicp每日一題[2.47]

再思即可發表於2024-10-17

Exercise 2.47

Here are two possible constructors for frames:

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

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

For each constructor supply the appropriate selectors to produce an implementation for frames.


這道題也不難,感覺是為了讓我們熟悉 car 和 cdr 這兩個函式。

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

(define (origin-frame-by-list frame)
  (car frame))

(define (edge1-frame-by-list frame)
  (cadr frame))

(define (edge2-frame-by-list frame)
  (caddr frame))

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

(define (origin-frame-by-cons frame)
  (car frame))

(define (edge1-frame-by-cons frame)
  (cadr frame))

(define (edge2-frame-by-cons frame)
  (cddr frame))


(define origin (make-vect 1 1))
(define v1 (make-vect 3 4))
(define v2 (make-vect -4 3))

(define frame1 (make-frame-by-list origin v1 v2))
(origin-frame-by-list frame1)
(edge1-frame-by-list frame1)
(edge2-frame-by-list frame1)

(define frame2 (make-frame-by-cons origin v1 v2))
(origin-frame-by-cons frame2)
(edge1-frame-by-cons frame2)
(edge2-frame-by-cons frame2)

; 執行結果
(mcons 1 1)
(mcons 3 4)
(mcons -4 3)
(mcons 1 1)
(mcons 3 4)
(mcons -4 3)

相關文章