1 / 13

데이타를 여러방식으로 구현할 때 Multiple Representations

데이타를 여러방식으로 구현할 때 Multiple Representations. 데이터 구현방식마다 꼬리표를 붙이고 . 데이터 속내용 감추기 원리를 유지하면서 . 예를 들어 complex number data 구현방안들을 생각해 보자 complex number 만들기 make-from-real-imag: real * real -> complex make-from-mag-angle: real * real -> complex complex number 사용하기 is-complex?:  -> bool

terra
Download Presentation

데이타를 여러방식으로 구현할 때 Multiple Representations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 데이타를 여러방식으로 구현할 때Multiple Representations • 데이터 구현방식마다 꼬리표를 붙이고. • 데이터 속내용 감추기 원리를 유지하면서. • 예를 들어 complex number data 구현방안들을 생각해 보자 • complex number 만들기 • make-from-real-imag: real * real -> complex • make-from-mag-angle: real * real -> complex • complex number 사용하기 • is-complex?:  -> bool • real: complex -> real • imag: complex -> real • mag: complex -> real • angle: complex -> real

  2. add-complex mul-complex make-from-real-imag make-from-mag-angle real img mag angle is-complex? rectangular representation

  3. (define (make-from-real-imag x y) (cons x y)) (define (make-from-mag-angle m a) (cons (* m (cos a)) (* m (sin a)))) (define (is-complex? c) (and (pair? c) (real? (car c)) (real? (cdr c))) (define (real c) (cond ((not (is-complex? c)) (error)) (else (car c)))) (define (imag c) (cond ((not (is-complex? c) (error)) (else (cdr c)))) (define (mag c) (cond ((not (is-complex? c) (error)) (else (sqrt (+ (square (real c)) (square (imag c))))) )) (define (angle c) (cond ((not (is-complex? c) (error)) (else (atan (imag c) (real c)))))

  4. 외부에서는, (define (add-complex c1 c2) (make-from-real-imag (+ (real c1) (real c2)) (+ (imag c1) (imag c2)) )) (define (mul-complex c1 c2) (make-from-mag-angle (* (mag c1) (mag c2)) (+ (angle c1) (angle c2)) ))

  5. add-complex mul-complex make-from-real-imag make-from-mag-angle real imag mag angle is-complex? make… make … rectangular representation polar representation

  6. rectagular complex 속내용 감추기data abstraction • make-from-real-imag-rectangular: real * real -> complex • make-from-mag-angle-rectangular: real * real -> complex • is-rectangular?: complex -> bool • real-rectangular: complex -> real • imag-rectangular: complex -> real • mag-rectangular: complex -> real • angle-rectangular: complex -> real (define rectangular-tag ‘rectangular) (define (make-from-real-imag-rectangular x y) (cons rectangular-tag (cons x y))) (define (make-from-mag-angle-rectangular m a) (cons rectangular-tag (cons (* m (cos a)) (* m (sin a))))) (define (is-rectangular? c) (equal? (car c) rectangular-tag)) (define (real-rectangular c) (car (cdr c))) (define (imag-rectangular c) (cdr (cdr c))) (define (mag-rectangular c) (sqrt (+ (square (real-rectangular c))) (+ (square (imag-rectangular c))))) (define (angle-rectangular c) (atan (imag-rectangular c) (real-rectangular c)))

  7. polar complex 속내용 감추기data abstraction • make-from-real-imag-polar: real * real -> complex • make-from-mag-angle-polar: real * real -> complex • is-polar?: complex -> bool • real-polar: complex -> real • imag-polar: complex -> real • mag-polar: complex -> real • angle-polar: complex -> real (define polar-tag ‘polar) (define (make-from-real-imag-polar x y) (cons polar-tag (cons …))) (define (make-from-mag-angle-polar m a) (cons polar-tag (cons m a))) (define (is-polar? c) (equal? (car c) polar-tag)) (define (mag-polar c) (car (cdr c))) (define (angle-polar c) (cdr (cdr c))) (define (real-polar c) (* (mag-polar c) (cos (angle-polar c)))) (define (imag-polarr c) (* (mag-polar c) (sin (angle-polar c))))

  8. (define (make-from-real-imag x y) (make-from-real-imag-rectangular x y)) (define (make-from-mag-angle m a) (make-from-mag-angle-polar m a)) (define (is-complex? c) (and (pair? c) (cond ((is-rectangular? c) (and (real? (real-rectangular c)) (real? (imag-rectangular c)))) ((is-polar? c) (and (real? (mag-polar c)) (real? (angle-polar c)))) (else #f)) ))

  9. (define (real c) (cond ((not (is-complex? c)) (error)) ((is-rectangular? c) (real-rectangular c)) ((is-polar? c) (real-polar c)))) (define (imag c) (cond ((not (is-complex? c)) (error)) ((is-rectangular? c) (imag-rectangular c)) ((is-polar? c) (imag-polar c)))) (define (mag c) (cond ((not (is-complex? c)) (error)) ((is-rectangllar? c) (mag-rectangular c)) ((is-polar? c) (mag-polar c)))) (define (angle c) (cond ((not (is-complex? c)) (error)) ((is-rectangular? c) (angle-rectangular c)) ((is-polar? c) (angle-polar c))))

  10. 외부에서는, 변함없이 (define (add-complex c1 c2) (make-from-real-imag (+ (real c1) (real c2)) (+ (imag c1) (imag c2)) )) (define (mul-complex c1 c2) (make-from-mag-angle (* (mag c1) (mag c2)) (+ (angle c1) (angle c2)) ))

  11. 데이타를 여러방식으로 구현할 때Multiple Representations • 데이터 구현방식마다 꼬리표를 붙이고. • 데이터 속내용 감추기 원리를 유지하면서. add-complex mul-complex make-from-real-imag make-from-mag-angle real imag mag angle is-complex? make … make… rectangular representation polar representation

  12. 또다른 complex number 구현방식을 품고 있게 하려면? add-complex mul-complex make-from-real-imag make-from-mag-angle real imag mag angle is-complex? make … make… make… polar representation xyz representation rectangular representation 어디를 어떻게 바꾸어야 할까? 적어도real, imag, mag, angle, is-complex?를 확장해야.

  13. (define (real c) (cond ((not (is-complex? c)) (error)) ((is-rectangular? c) (real-rectangular c)) ((is-polar? c) (real-polar c)) ((is-xyz? c) (real-xyz c)) )) (define (imag c) (cond ((not (is-complex? c)) (error)) ((is-rectangular? c) (imag-rectangular c)) ((is-polar? c) (imag-polar c)) ((is-xyz? c) (imag-xyz c)) )) (define (mag c) (cond ((not (is-complex? c)) (error)) ((is-rectangllar? c) (mag-rectangular c)) ((is-polar? c) (mag-polar c)) ((is-xyz? c) (mag-xyz c)) )) (define (angle c) (cond ((not (is-complex? c)) (error)) ((is-rectangular? c) (angle-rectangular c)) ((is-polar? c) (angle-polar c)) ((is-xyz? c) (angle-xyz c)) ))

More Related