乐正

Actions speak louder than words.

Sicp-ex2-47

问题

下面是实现框架的两个可能的过程函数:

1
2
3
4
5
(define (make-frame origin edge1 edge2)
  (list origin edge1 edge2))

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

请为每个构造函数提供适当的选择函数,为框架做出相应的实现。

解答

练习2.47 (ex2-47.scm) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
;; 第一种
(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)
  (cadr (cdr frame)))

;; 第二种
(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)
  (cdr (cdr frame)))

draft

« sicp-ex2-46 sicp-ex2-48 »

Comments