sicp2.4消息传递总结

消息传递总结

;1.数据对象如何表示?
;使用过程表示数据
;2.该过程使用什么作为输入?
;使用操作作为输入,输入该操作后,执行指定操作
;3.通用过程如何表示?
;对参数(该参数为一个数据对象),应用一个操作
;
;总结,每个数据对象中,都有很多种操作
;添加一种新的数据对象,只需要添加一个新的数据对象就可以,代码修改比较少
;添加一种新的操作,需要在所有的数据对象中添加,代码修改相对较多


;消息传递
(define (square x) (* x x))

(define (add-complex z1 z2)
  (make-from-real-imag (+ (real-part z1) (real-part z2))
                       (+ (imag-part z1) (imag-part z2))))

(define (sub-complex z1 z2)
  (make-from-real-imag (- (real-part z1) (real-part z2))
                       (- (imag-part z1) (imag-part z2))))

(define (mul-complex z1 z2)
  (make-from-mag-ang (* (magnitude z1) (magnitude z2))
                     (+ (angle z1) (angle z2))))

(define (div-complex z1 z2)
  (make-from-mag-ang (/ (magnitude z1) (magnitude z2))
                     (- (angle z1) (angle z2))))

;apply-generic调用arg过程
(define (apply-generic op arg) (arg op))


(define (real-part z) (apply-generic 'real-part z))
(define (imag-part z) (apply-generic 'imag-part z))
(define (magnitude z) (apply-generic 'magnitude z))
(define (angle z)     (apply-generic 'angle z))

;1.数据对象如何表示?
;使用过程表示数据
;2.该过程使用什么作为输入?
;使用操作作为输入,输入该操作后,执行指定操作
;3.通用过程如何表示?
;对参数(该参数为一个数据对象),应用一个操作
;
;总结,每个数据对象中,都有很多种操作
;添加一种新的数据对象,只需要添加一个新的数据对象就可以,代码修改比较少
;添加一种新的操作,需要在所有的数据对象中添加,代码修改相对较多




;使用数据抽象来表示数据
;(real-part (make-from-real-imag 10 20)),结果为10
;((make-from-real-imag 10 20) 'real-part),即给(make-from-real-imag 10 20)传递一个op
(define (make-from-real-imag x y)
  (define (dispatch op)
    (cond ((eq? op 'real-part) x)
          ((eq? op 'imag-part) y)
          ((eq? op 'magnitude) (sqrt (+ (square x) (square y))))
          ((eq? op 'angle) (atan y x))
          (else 
            (error "Unkown op -- MAKE-FROM-REAL-IMAG" op))))
  dispatch)

(define (make-from-mag-ang r a)
  (define (dispatch op)
    (cond ((eq? op 'real-part) (* r (cos a)))
          ((eq? op 'imag-part) (* r (sin a)))
          ((eq? op 'magnitude) r)
          ((eq? op 'angle) a)
          (else 
            (error "Unkown op -- MAKE-FROM-MAG-ANG" op))))
  dispatch)

;;;;;;;;;;;;;;;;;;;
(define a (make-from-real-imag 10 20))
(define b (make-from-real-imag 1 2))
(define c (make-from-mag-ang (magnitude a) (angle a)))
(define d (make-from-mag-ang (magnitude b) (angle b)))

(define (print-complex z)
  (define (print-pair v0 v1)
    (display "(")
    (display v0)
    (display ", ")
    (display v1)
    (display ")"))
  
  (display "(r, a) = ")
  (print-pair (magnitude z) (angle z))
  (display "     (x, y) = ")
  (print-pair (real-part z) (imag-part z))
  (newline)
  )

(print-complex (add-complex a b))
(print-complex (sub-complex a b))
(print-complex (mul-complex a b))
(print-complex (div-complex a b))

(print-complex (add-complex c d))
(print-complex (sub-complex c d))
(print-complex (mul-complex c d))
(print-complex (div-complex c d))

;数据对象是一个实体
;这个实体以消息的方式接收一个操作

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值