SICP Exercise 연습문제 2.79

By | 2008/02/27

이 문제는 두 수가 같은지 확인하는 equ? 프로시저를 정의하는 문제입니다.

 

c8

잘 되는군요.^^

 

특히 real-imag와 mag-ang로 같은 복소수를 표현해도 잘 처리가 되었습니다.

 

복소수의 경우 real과 imaginary가 같은지 확인하고,

or 연산으로 magnitude와 angle이 같은지도 확인합니다.

굳이 할 필요는 없지만,

어차피 Lazy evaluation이라면 뒤의 것은 수행하지 않을터이니

성능에는 큰 무리가 없을 듯싶습니다.

하지만 혹시나 하는 생각에 처리하였습니다.

 

 

참조

해럴드 애빌슨, 김재우 역, <컴퓨터 프로그램의 구조와 해석>, 인사이트, 2007, pp. 251

 

 

(define true (= 0 0))
(define false (= 0 1))
(define (square x) (* x x))
; put/get
; in ch2support.scm – MIT support
(define (assoc key records)
  (cond ((null? records) false)
        ((equal? key (caar records)) (car records))
        (else (assoc key (cdr records)))))
(define (make-table)
  (let ((local-table (list ‘*table*)))
    (define (lookup key-1 key-2)
      (let ((subtable (assoc key-1 (cdr local-table))))
        (if subtable
            (let ((record (assoc key-2 (cdr subtable))))
              (if record
                  (cdr record)
                  false))
            false)))
    (define (insert! key-1 key-2 value)
      (let ((subtable (assoc key-1 (cdr local-table))))
        (if subtable
            (let ((record (assoc key-2 (cdr subtable))))
              (if record
                  (set-cdr! record value)
                  (set-cdr! subtable
                            (cons (cons key-2 value)
                                  (cdr subtable)))))
            (set-cdr! local-table
                      (cons (list key-1
                                  (cons key-2 value))
                            (cdr local-table)))))
      ‘ok)   
    (define (dispatch m)
      (cond ((eq? m ‘lookup-proc) lookup)
            ((eq? m ‘insert-proc!) insert!)
            (else (error “Unknown operation — TABLE” m))))
    dispatch))
(define operation-table (make-table))
(define get (operation-table ‘lookup-proc))
(define put (operation-table ‘insert-proc!))

; section 2.5.1
(define (add x y) (apply-generic ‘add x y))
(define (sub x y) (apply-generic ‘sub x y))
(define (mul x y) (apply-generic ‘mul x y))
(define (div x y) (apply-generic ‘div x y))

; 기본 수
(define (install-scheme-number-package)
  (define (tag x)
    (attach-tag ‘scheme-number x))
  (put ‘add ‘(scheme-number scheme-number)
       (lambda (x y) (tag (+ x y))))
  (put ‘sub ‘(scheme-number scheme-number)
       (lambda (x y) (tag (- x y))))
  (put ‘mul ‘(scheme-number scheme-number)
       (lambda (x y) (tag (* x y))))
  (put ‘div ‘(scheme-number scheme-number)
       (lambda (x y) (tag (/ x y))))
  (put ‘make ‘scheme-number
       (lambda (x) (tag x)))
  ; exercise 2.79 answer
  (put ‘equ? ‘(scheme-number scheme-number)
       (lambda (x y) (= x y)))
  ‘done)

(define (make-scheme-number n)
  ((get ‘make ‘scheme-number) n))

; 유리수
(define (install-rational-package)
  ; 프로시저
  (define (numer x) (car x))
  (define (denom x) (cdr x))
  (define (make-rat n d)
    (let ((g (gcd n d)))
      (cons (/ n g) (/ d g))))
  (define (add-rat x y)
    (make-rat (+ (* (numer x) (denom y))
                 (* (numer y) (denom x)))
              (* (denom x) (denom y))))
  (define (sub-rat x y)
    (make-rat (- (* (numer x) (denom y))
                 (* (numer y) (denom x)))
              (* (denom x) (denom y))))
  (define (mul-rat x y)
    (make-rat (* (numer x) (numer y))
              (* (denom x) (denom y))))
  (define (div-rat x y)
    (make-rat (* (numer x) (denom y))
              (* (denom x) (numer y))))
  ; 인터페이스
  (define (tag x) (attach-tag ‘rational x))
  (put ‘add ‘(rational rational)
       (lambda (x y) (tag (add-rat x y))))
  (put ‘sub ‘(rational rational)
       (lambda (x y) (tag (sub-rat x y))))
  (put ‘mul ‘(rational rational)
       (lambda (x y) (tag (mul-rat x y))))
  (put ‘div ‘(rational rational)
       (lambda (x y) (tag (div-rat x y))))
  (put ‘make ‘rational
       (lambda (n d) (tag (make-rat n d))))
  ; exercise 2.79 answer
  (put ‘equ? ‘(rational rational)
       (lambda (x y)
         (and (= (numer x) (numer y)) (= (denom x) (denom y)))))
  ‘done)

(define (make-rational n d)
  ((get ‘make ‘rational) n d))

; 복소수
(define (install-complex-package)
  ; 프로시저
  (define (make-from-real-imag x y)
    ((get ‘make-from-real-imag ‘rectangular) x y))
  (define (make-from-mag-ang r a)
    ((get ‘make-from-mag-ang ‘polar) r a))
  (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))))
  ; 인터페이스
  (define (tag z) (attach-tag ‘complex z))
  (put ‘add ‘(complex complex)
       (lambda (z1 z2) (tag (add-complex z1 z2))))
  (put ‘sub ‘(complex complex)
       (lambda (z1 z2) (tag (sub-complex z1 z2))))
  (put ‘mul ‘(complex complex)
       (lambda (z1 z2) (tag (mul-complex z1 z2))))
  (put ‘div ‘(complex complex)
       (lambda (z1 z2) (tag (div-complex z1 z2))))
  (put ‘make-from-real-imag ‘complex
       (lambda (x y) (tag (make-from-real-imag x y))))
  (put ‘make-from-mag-ang ‘complex
       (lambda (r a) (tag (make-from-mag-ang r a))))
  ; add exercise 2.77
  (put ‘real-part ‘(complex) real-part)
  (put ‘imag-part ‘(complex) imag-part)
  (put ‘magnitude ‘(complex) magnitude)
  (put ‘angle ‘(complex) angle)
  ; exercise 2.79 answer
  (put ‘equ? ‘(complex complex)
       (lambda (x y)
         (or (and (= (real-part x) (real-part y))
                  (= (imag-part x) (imag-part y)))
             (and (= (magnitude x) (magnitude y))
                  (= (angle x) (angle y))))))
  ‘done)

(define (make-complex-from-real-imag x y)
  ((get ‘make-from-real-imag ‘complex) x y))
(define (make-complex-from-mag-ang r a)
  ((get ‘make-from-mag-ang ‘complex) r a))

; section 2.4
; 직각 좌표계
(define (install-rectangular-package)
  ; 프로시저
  (define (real-part z) (car z))
  (define (imag-part z) (cdr z))
  (define (make-from-real-imag x y) (cons x y))
  (define (magnitude z)
    (sqrt (+ (square (real-part z))
             (square (imag-part z)))))
  (define (angle z)
    (atan (imag-part z) (real-part z)))
  (define (make-from-mag-ang r a)
    (cons (* r (cos a)) (* r (sin a))))
  ; 인터페이스
  (define (tag x) (attach-tag ‘rectangular x))
  (put ‘real-part ‘(rectangular) real-part)
  (put ‘imag-part ‘(rectangular) imag-part)
  (put ‘magnitude ‘(rectangular) magnitude)
  (put ‘angle ‘(rectangular) angle)
  (put ‘make-from-real-imag ‘rectangular
       (lambda (x y) (tag (make-from-real-imag x y))))
  (put ‘make-from-mag-ang ‘rectangular
       (lambda (r a) (tag (make-from-mag-ang r a))))
  ‘done)

; 극좌표계
(define (install-polar-package)
  ; 프로시저
  (define (magnitude z) (car z))
  (define (angle z) (cdr z))
  (define (make-from-mag-ang r a) (cons r a))
  (define (real-part z)
    (* (magnitude z) (cos (angle z))))
  (define (imag-part z)
    (* (magnitude z) (sin (angle z))))
  (define (make-from-real-imag x y)
    (cons (sqrt (+ (square x) (square y)))
          (atan y x)))
  ; 인터페이스
  (define (tag x) (attach-tag ‘polar x))
  (put ‘real-part ‘(polar) real-part)
  (put ‘imag-part ‘(polar) imag-part)
  (put ‘magnitude ‘(polar) magnitude)
  (put ‘angle ‘(polar) angle)
  (put ‘make-from-real-imag ‘polar
       (lambda (x y) (tag (make-from-real-imag x y))))
  (put ‘make-from-mag-ang ‘polar
       (lambda (r a) (tag (make-from-mag-ang r a))))
  ‘done)

(define (apply-generic op . args)
  (let ((type-tags (map type-tag args)))
    (let ((proc (get op type-tags)))
      (if proc
          (apply proc (map contents args))
          (error
           “No method for these types — APPLY-GENERIC”
           (list op type-tags))))))
(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))

; exercise 2.78
(define (attach-tag type-tag contents)
  (if (number? contents)
      contents
      (cons type-tag contents)))
(define (type-tag datum)
  (cond ((pair? datum) (car datum))
        ((number? datum) ‘scheme-number)
        (else (error “Bad tagged datum — TYPE-TAG” datum))))
(define (contents datum)
  (cond ((pair? datum) (cdr datum))
        ((number? datum) datum)
        (else (error “Bad tagged datum — CONTENTS” datum))))

; answer
(define (equ? a b) (apply-generic ‘equ? a b))
; ordinary numbers
;  (put ‘equ? ‘(scheme-number scheme-number)
;       (lambda (x y) (= x y)))
; rational numbers
;  (put ‘equ? ‘(rational rational)
;       (lambda (x y)
;         (and (= (numer x) (numer y)) (= (denom x) (denom y)))))
; complex numbers
;  (put ‘equ? ‘(complex complex)
;       (lambda (x y)
;         (or (and (= (real-part x) (real-part y))
;                  (= (imag-part x) (imag-part y)))
;             (and (= (magnitude x) (magnitude y))
;                  (= (angle x) (angle y))))))
; execute
(install-scheme-number-package)
(install-rational-package)
(install-complex-package)
(install-rectangular-package)
(install-polar-package)
(newline)
(define n1 (make-scheme-number 6))
(define n2 (make-scheme-number 2))
(define r1 (make-rational 3 4))
(define r2 (make-rational 4 5))
(define z1 (make-complex-from-real-imag 3 4))
(define z2 (make-complex-from-real-imag 4 5))
(define z3 (make-complex-from-mag-ang 5 (atan 4 3)))
n1
n2
r1
r2
z1
z2
z3
(newline)
(equ? n1 n1)
(equ? n1 n2)
(equ? r1 r1)
(equ? r1 r2)
(equ? z1 z1)
(equ? z1 z2)
(equ? z1 z3)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.