乐正

Actions speak louder than words.

Sicp-ex2-75

问题

请用消息传递的风格实现构造函数make-from-mag-ang。这个过程应该与上面给出的过程 make-from-real-imag过程类似。

解答

1
2
3
4
5
6
7
8
(define (make-from-mag-ang r a)
  (define (dispatch op)
    (cond ((eq? op 'real-part) (* r (sin a)))
          ((eq? op 'image-part) (* r (cos a)))
          ((eq? op 'magnitude) r)
          ((eq? op 'angle) a)
          (else (error "Unkown op -- MAKE-FROM-MAG-ANG" op))))
  dispatch)

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(define m (make-from-mag-ang 10 45))
;Value: m

(m 'real-part)
;Value: 8.509035245341185

(m 'image-part)
;Value: 5.253219888177298

(m 'magnitude)
;Value: 10

(m 'angle)
;Value: 45

(m 'unknown)
;Unkown op -- MAKE-FROM-MAG-ANG unknown

draft

« sicp-ex2-74 sicp-ex2-76 »

Comments