sicp_2.5 일반화된 연산시스템

25
SICP 2.5 일반화된 연산 시스템 cecil

Upload: hyeonseok-choi

Post on 04-Jul-2015

226 views

Category:

Technology


0 download

DESCRIPTION

컴퓨터 프로그램 구조와 해석 2.5절.

TRANSCRIPT

Page 1: SICP_2.5 일반화된 연산시스템

SICP����������� ������������������  2.5����������� ������������������  일반화된����������� ������������������  연산����������� ������������������  시스템

cecil

Page 2: SICP_2.5 일반화된 연산시스템

한����������� ������������������  데이터의����������� ������������������  표현이����������� ������������������  서로����������� ������������������  다를����������� ������������������  때는����������� ������������������  물론이고,����������� ������������������  아예����������� ������������������  인자의����������� ������������������  

종류가����������� ������������������  다른����������� ������������������  경우라도����������� ������������������  이를����������� ������������������  모두����������� ������������������  처리하는����������� ������������������  연산을����������� ������������������  정의

Page 3: SICP_2.5 일반화된 연산시스템

296

Figure 2.23 shows the structure of the system we shall build. Noticethe abstraction barriers. From the perspective of someone using“numbers,” there is a single procedure add that operates on whatevernumbers are supplied. Add is part of a generic interface that allows theseparate ordinary-arithmetic, rational-arithmetic and complex-arithmeticpackages to be accessed uniformly by programs that use numbers. Anyindividual arithmetic package (such as the complex package)may itself beaccessed through generic procedures (such as add-complex) that combinepackages designed for different representations (such as rectangular andpolar). Moreover, the structure of the system is additive, so that one candesign the individual arithmetic packages separately and combine themto produce a generic arithmetic system.

Figure 2.23: Generic arithmetic system.

일반화된����������� ������������������  산술����������� ������������������  연산

Page 4: SICP_2.5 일반화된 연산시스템

•2.4����������� ������������������  절의����������� ������������������  Mapping����������� ������������������  Table����������� ������������������  구조를����������� ������������������  사용����������� ������������������  

(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))

일반화된����������� ������������������  산술����������� ������������������  프로시저����������� ������������������  정의

Page 5: SICP_2.5 일반화된 연산시스템

(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)))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ’done)

정수����������� ������������������  연산을����������� ������������������  위한����������� ������������������  패키지

Number를����������� ������������������  위한����������� ������������������  연산을����������� ������������������  제공하기����������� ������������������  때문에����������� ������������������  ����������� ������������������  scheme-number����������� ������������������  scheme-number����������� ������������������  인덱스����������� ������������������  사용

Page 6: SICP_2.5 일반화된 연산시스템

define����������� ������������������  (install-rational-package)����������� ������������������  ;;����������� ������������������  internal����������� ������������������  procedures����������� ������������������  ����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (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))))����������� ������������������  

;;����������� ������������������  interface����������� ������������������  to����������� ������������������  rest����������� ������������������  of����������� ������������������  the����������� ������������������  system����������� ������������������  ����������� ������������������  

(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))))����������� ������������������  ’done)����������� ������������������  

유리수����������� ������������������  연산을����������� ������������������  위한����������� ������������������  패키지

Page 7: SICP_2.5 일반화된 연산시스템

define����������� ������������������  (install-rational-package)����������� ������������������  ;;����������� ������������������  internal����������� ������������������  procedures����������� ������������������  ����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (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))))����������� ������������������  

;;����������� ������������������  interface����������� ������������������  to����������� ������������������  rest����������� ������������������  of����������� ������������������  the����������� ������������������  system����������� ������������������  ����������� ������������������  

(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))))����������� ������������������  ’done)����������� ������������������  

유리수����������� ������������������  연산을����������� ������������������  위한����������� ������������������  패키지

Page 8: SICP_2.5 일반화된 연산시스템

(define����������� ������������������  (install-complex-package)����������� ������������������  ;;����������� ������������������  imported����������� ������������������  procedures����������� ������������������  from����������� ������������������  rectangular����������� ������������������  and����������� ������������������  polar����������� ������������������  packages����������� ������������������  ����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (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))����������� ������������������  ;;����������� ������������������  internal����������� ������������������  procedures����������� ������������������  ����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (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))))

;;����������� ������������������  interface����������� ������������������  to����������� ������������������  rest����������� ������������������  of����������� ������������������  the����������� ������������������  system����������� ������������������  ����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (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))))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ’done)����������� ������������������  

복소수����������� ������������������  연산을����������� ������������������  위한����������� ������������������  패키지

Page 9: SICP_2.5 일반화된 연산시스템

현재까지����������� ������������������  진행된����������� ������������������  것들…⋯����������� ������������������  

서로����������� ������������������  다른����������� ������������������  패키지를����������� ������������������  설치하여����������� ������������������  전혀����������� ������������������  다른����������� ������������������  타입����������� ������������������  

(정수,����������� ������������������  유리수,����������� ������������������  복소수)에����������� ������������������  대해서도����������� ������������������  동일한����������� ������������������  인터페이스����������� ������������������  

(add,����������� ������������������  sub,����������� ������������������  mul,����������� ������������������  div)를����������� ������������������  사용하여����������� ������������������  산술����������� ������������������  연산����������� ������������������  가능����������� ������������������  

그럼.����������� ������������������  타입이����������� ������������������  다른����������� ������������������  데이터를����������� ������������������  같이����������� ������������������  쓰려면??(cross-type)

Page 10: SICP_2.5 일반화된 연산시스템

동일한����������� ������������������  방법으로����������� ������������������  복소수,����������� ������������������  정수����������� ������������������  연산

;;����������� ������������������  to����������� ������������������  be����������� ������������������  included����������� ������������������  in����������� ������������������  the����������� ������������������  complex����������� ������������������  package����������� ������������������  ����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (define����������� ������������������  (add-complex-to-schemenum����������� ������������������  z����������� ������������������  x)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (make-from-real-imag����������� ������������������  (+����������� ������������������  (real-part����������� ������������������  z)����������� ������������������  x)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (imag-part����������� ������������������  z)))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (put����������� ������������������  ’add����������� ������������������  ’(complex����������� ������������������  scheme-number)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (lambda����������� ������������������  (z����������� ������������������  x)����������� ������������������  (tag����������� ������������������  (add-complex-to-schemenum����������� ������������������  z����������� ������������������  x))))

문제점����������� ������������������  

•모든����������� ������������������  타입에����������� ������������������  대해����������� ������������������  cross-type����������� ������������������  연산을����������� ������������������  만들려면,����������� ������������������  타입수N:����������� ������������������  N����������� ������������������  *����������� ������������������  (N����������� ������������������  -1)개를����������� ������������������  구현해야����������� ������������������  함����������� ������������������  ����������� ������������������  

•이����������� ������������������  연산을����������� ������������������  어느����������� ������������������  패키지에����������� ������������������  넣을지����������� ������������������  판단하는����������� ������������������  것도����������� ������������������  쉽지����������� ������������������  않음.

Page 11: SICP_2.5 일반화된 연산시스템

타입����������� ������������������  바꾸기

데이터����������� ������������������  타입이����������� ������������������  서로����������� ������������������  완전히����������� ������������������  독립되지����������� ������������������  않아서,����������� ������������������  한����������� ������������������  데이터����������� ������������������  타입을����������� ������������������  ����������� ������������������  

다른����������� ������������������  데이터����������� ������������������  타입으로����������� ������������������  바꿀����������� ������������������  수����������� ������������������  있는����������� ������������������  경우,����������� ������������������  이����������� ������������������  방법을����������� ������������������  타입����������� ������������������  바꾸기라����������� ������������������  함.����������� ������������������  

이를����������� ������������������  통해����������� ������������������  기존의����������� ������������������  패키지를����������� ������������������  이용해서����������� ������������������  연산이����������� ������������������  가능.

ex)����������� ������������������  정수����������� ������������������  ->����������� ������������������  복소수����������� ������������������  타입����������� ������������������  변환����������� ������������������  프로시저

����������� ������������������  (define����������� ������������������  (scheme-number->complex����������� ������������������  n)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (make-complex-from-real-imag����������� ������������������  (contents����������� ������������������  n)����������� ������������������  0))

Page 12: SICP_2.5 일반화된 연산시스템

타입����������� ������������������  바꾸기를����������� ������������������  적용한����������� ������������������  apply-generic(define����������� ������������������  (apply-generic����������� ������������������  op����������� ������������������  .����������� ������������������  args)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (let����������� ������������������  ((type-tags����������� ������������������  (map����������� ������������������  type-tag����������� ������������������  args)))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  args의����������� ������������������  타입을����������� ������������������  구함����������� ������������������  ex)����������� ������������������  complex����������� ������������������  scheme-number����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (let����������� ������������������  ((proc����������� ������������������  (get����������� ������������������  op����������� ������������������  type-tags)))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  complex����������� ������������������  scheme-number을����������� ������������������  위한����������� ������������������  연산을����������� ������������������  찾음.����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (if����������� ������������������  proc����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (apply����������� ������������������  proc����������� ������������������  (map����������� ������������������  contents����������� ������������������  args))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  연산이����������� ������������������  있을����������� ������������������  경우����������� ������������������  실행����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (if����������� ������������������  (=����������� ������������������  (length����������� ������������������  args)����������� ������������������  2)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (let����������� ������������������  ((type1����������� ������������������  (car����������� ������������������  type-tags))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  첫번째����������� ������������������  변수의����������� ������������������  타입을����������� ������������������  구함����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (type2����������� ������������������  (cadr����������� ������������������  type-tags))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  두번째����������� ������������������  변수의����������� ������������������  타입을����������� ������������������  구함����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (a1����������� ������������������  (car����������� ������������������  args))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  첫����������� ������������������  번째����������� ������������������  변수를����������� ������������������  a1����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (a2����������� ������������������  (cadr����������� ������������������  args)))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  두����������� ������������������  번째����������� ������������������  변수를����������� ������������������  a2����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (let����������� ������������������  ((t1->t2����������� ������������������  (get-coercion����������� ������������������  type1����������� ������������������  type2))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  t1->����������� ������������������  t2로����������� ������������������  변환하는����������� ������������������  함수를����������� ������������������  찾음.����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (t2->t1����������� ������������������  (get-coercion����������� ������������������  type2����������� ������������������  type1)))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  t2->����������� ������������������  t1으로����������� ������������������  변환하는����������� ������������������  함수를����������� ������������������  찾음.����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (cond����������� ������������������  (t1->t2����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (apply-generic����������� ������������������  op����������� ������������������  (t1->t2����������� ������������������  a1)����������� ������������������  a2))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  t1이����������� ������������������  있을����������� ������������������  경우����������� ������������������  a1의����������� ������������������  타입을����������� ������������������  변환하여����������� ������������������  연산����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (t2->t1����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (apply-generic����������� ������������������  op����������� ������������������  a1����������� ������������������  (t2->t1����������� ������������������  a2)))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  t2가����������� ������������������  있을����������� ������������������  경우����������� ������������������  a2의����������� ������������������  타입을����������� ������������������  변환하여����������� ������������������  연산����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (else����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (error����������� ������������������  "No����������� ������������������  method����������� ������������������  for����������� ������������������  these����������� ������������������  types"����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (list����������� ������������������  op����������� ������������������  type-tags))))))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (error����������� ������������������  "No����������� ������������������  method����������� ������������������  for����������� ������������������  these����������� ������������������  types"����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (list����������� ������������������  op����������� ������������������  type-tags)))))))

Page 13: SICP_2.5 일반화된 연산시스템

타입����������� ������������������  바꾸기����������� ������������������  연산을����������� ������������������  적용할����������� ������������������  경우����������� ������������������  

타입과����������� ������������������  타입을����������� ������������������  연관����������� ������������������  짓기����������� ������������������  위해����������� ������������������  N2개의����������� ������������������  타입����������� ������������������  바꾸기����������� ������������������  

프로시저를����������� ������������������  짜는����������� ������������������  것은����������� ������������������  어쩔수����������� ������������������  없는����������� ������������������  일이지만,����������� ������������������  데이터����������� ������������������  타입����������� ������������������  뭉치와����������� ������������������  

일반화된����������� ������������������  연산마다����������� ������������������  따로따로����������� ������������������  프로시저를����������� ������������������  정의할����������� ������������������  필요����������� ������������������  없이����������� ������������������  

타입����������� ������������������  한쌍에����������� ������������������  프로시저����������� ������������������  하나씩만����������� ������������������  정의하면����������� ������������������  됨.

만약,����������� ������������������  엮어����������� ������������������  쓰려고����������� ������������������  하는����������� ������������������  두����������� ������������������  타입����������� ������������������  사이에����������� ������������������  서로����������� ������������������  어느쪽으로도����������� ������������������  바꿀����������� ������������������  방법이����������� ������������������  없다면?

Page 14: SICP_2.5 일반화된 연산시스템

타입의����������� ������������������  계층����������� ������������������  관계

서로����������� ������������������  다른����������� ������������������  데이터����������� ������������������  타입을����������� ������������������  연관����������� ������������������  짓는����������� ������������������  방법에서����������� ������������������  ����������� ������������������  

더����������� ������������������  ‘폭넓은’����������� ������������������  체계를����������� ������������������  이끌어����������� ������������������  낼수����������� ������������������  있는����������� ������������������  경우가����������� ������������������  종종����������� ������������������  있음.����������� ������������������  (Tower����������� ������������������  

위����������� ������������������  타입:����������� ������������������  supertype,����������� ������������������  아래����������� ������������������  타입:����������� ������������������  subtype

310

Figure 2.25: A tower of types.

If we have a tower structure, then we can greatly simplify the problem ofadding a new type to the hierarchy, for we need only specify how the newtype is embedded in the next supertype above it and how it is the supertypeof the type below it. For example, if we want to add an integer to a com-plex number, we need not explicitly define a special coercion procedureinteger->complex. Instead, we define how an integer can be transformedinto a rational number, how a rational number is transformed into a realnumber, and how a real number is transformed into a complex number.We then allow the system to transform the integer into a complex numberthrough these steps and then add the two complex numbers.

We can redesign our apply-generic procedure in the following way: Foreach type, we need to supply a raise procedure, which “raises” objects ofthat type one level in the tower. Then when the system is required to op-erate on objects of different types it can successively raise the lower typesuntil all the objects are at the same level in the tower. (Exercise 2.83 andExercise 2.84 concern the details of implementing such a strategy.)

장점����������� ������������������  

•바로����������� ������������������  윗����������� ������������������  타입으로����������� ������������������  변환하는����������� ������������������  프로시저만����������� ������������������  있으면����������� ������������������  됨.����������� ������������������  

•아래����������� ������������������  타입이����������� ������������������  위����������� ������������������  타입에서����������� ������������������  정의한����������� ������������������  연산을����������� ������������������  모두����������� ������������������  물려����������� ������������������  받는다는����������� ������������������  개념을����������� ������������������  구현����������� ������������������  가능����������� ������������������  

•데이터를����������� ������������������  가장����������� ������������������  단순한����������� ������������������  표현으로����������� ������������������  끌어����������� ������������������  내릴����������� ������������������  수����������� ������������������  있음.����������� ������������������  (����������� ������������������  2+3i와����������� ������������������  4-3i의����������� ������������������  덧셈)

Page 15: SICP_2.5 일반화된 연산시스템

계층����������� ������������������  구조가����������� ������������������  지닌����������� ������������������  문제점313

Figure 2.26: Relations among types of geometric figures.

Exercise 2.81: Louis Reasoner has noticed that apply-generic

may try to coerce the arguments to each other’s type even if theyalready have the same type. Therefore, he reasons, we needto put procedures in the coercion table to coerce arguments ofeach type to their own type. For example, in addition to thescheme-number->complex coercion shown above, he would do:

(define (scheme-number->scheme-number n) n)

(define (complex->complex z) z)

(put-coercion ’scheme-number ’scheme-number

scheme-number->scheme-number)

(put-coercion ’complex ’complex complex->complex)

계층����������� ������������������  구조가����������� ������������������  복잡할����������� ������������������  경우����������� ������������������  

타입����������� ������������������  변환하는데����������� ������������������  여러개의����������� ������������������  

방법이����������� ������������������  있어����������� ������������������  일반화된����������� ������������������  연산을����������� ������������������  

다루기����������� ������������������  쉽지����������� ������������������  않음!

Page 16: SICP_2.5 일반화된 연산시스템

연습:����������� ������������������  기호����������� ������������������  식����������� ������������������  대수����������� ������������������  

(다항식����������� ������������������  연산)

Page 17: SICP_2.5 일반화된 연산시스템

구현하고자����������� ������������������  하는����������� ������������������  것.����������� ������������������  

(5X3+����������� ������������������  X2+����������� ������������������  5)����������� ������������������  +����������� ������������������  (3X2+4)����������� ������������������  =����������� ������������������  5X3+4X2+9����������� ������������������  

(X2+����������� ������������������  5)����������� ������������������  *����������� ������������������  (3X)����������� ������������������  =����������� ������������������  3X3����������� ������������������  +����������� ������������������  15X

Page 18: SICP_2.5 일반화된 연산시스템

다항식����������� ������������������  정의하기

A:����������� ������������������  X5����������� ������������������  +����������� ������������������  2X4����������� ������������������  +����������� ������������������  3X2����������� ������������������  -����������� ������������������  2X����������� ������������������  -����������� ������������������  5����������� ������������������  

B:����������� ������������������  X100����������� ������������������  +����������� ������������������  2X2����������� ������������������  +����������� ������������������  1

고려사항:����������� ������������������  빽빽함(density)

1.����������� ������������������  계수를����������� ������������������  단순히����������� ������������������  리스트로����������� ������������������  묶어����������� ������������������  나타낼����������� ������������������  경우����������� ������������������  

A:����������� ������������������  (1����������� ������������������  ����������� ������������������  2����������� ������������������  ����������� ������������������  0����������� ������������������  ����������� ������������������  3����������� ������������������  ����������� ������������������  -2����������� ������������������  ����������� ������������������  -5)����������� ������������������  

B:����������� ������������������  (100����������� ������������������  ����������� ������������������  …⋯����������� ������������������  ����������� ������������������  2����������� ������������������  ����������� ������������������  0����������� ������������������  ����������� ������������������  1)

2.����������� ������������������  0이����������� ������������������  아닌����������� ������������������  계수만����������� ������������������  차수와����������� ������������������  묶어서����������� ������������������  표현할����������� ������������������  경우����������� ������������������  

A:����������� ������������������  ((5����������� ������������������  1)����������� ������������������  (4����������� ������������������  2)����������� ������������������  (2����������� ������������������  3)����������� ������������������  (1����������� ������������������  2)����������� ������������������  (0����������� ������������������  -5))����������� ������������������  

B:����������� ������������������  ((100����������� ������������������  1)����������� ������������������  (2����������� ������������������  2)����������� ������������������  (0����������� ������������������  1)) 채택!!

Page 19: SICP_2.5 일반화된 연산시스템

기본����������� ������������������  연산����������� ������������������  정의

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (define����������� ������������������  (make-term����������� ������������������  order����������� ������������������  coeff)����������� ������������������  (list����������� ������������������  order����������� ������������������  coeff))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  항����������� ������������������  하나를����������� ������������������  만듬.����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (define����������� ������������������  (order����������� ������������������  term)����������� ������������������  (car����������� ������������������  term))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  차수를����������� ������������������  구함����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (define����������� ������������������  (coeff����������� ������������������  term)����������� ������������������  (cadr����������� ������������������  term))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  계수를����������� ������������������  구함����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (define����������� ������������������  (adjoin-term����������� ������������������  term����������� ������������������  term-list)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  항을����������� ������������������  묶어����������� ������������������  다항식으로����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (if����������� ������������������  (=zero?����������� ������������������  (coeff����������� ������������������  term))����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  term-list����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (cons����������� ������������������  term����������� ������������������  term-list)))����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (define����������� ������������������  (the-empty-termlist)����������� ������������������  ’())����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  빈����������� ������������������  다항식����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (define����������� ������������������  (first-term����������� ������������������  term-list)����������� ������������������  (car����������� ������������������  term-list))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  첫번째����������� ������������������  항����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (define����������� ������������������  (rest-terms����������� ������������������  term-list)����������� ������������������  (cdr����������� ������������������  term-list))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ;����������� ������������������  나머지����������� ������������������  항����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (define����������� ������������������  (empty-termlist?����������� ������������������  term-list)����������� ������������������  (null?����������� ������������������  term-list))����������� ������������������  ;����������� ������������������  항이����������� ������������������  null인가?����������� ������������������  

중요:����������� ������������������  다항식을����������� ������������������  만들때����������� ������������������  순서에����������� ������������������  ����������� ������������������  맞게����������� ������������������  생성한다고����������� ������������������  가정

Page 20: SICP_2.5 일반화된 연산시스템

(define����������� ������������������  (install-polynomial-package)����������� ������������������  ;;����������� ������������������  패키지����������� ������������������  내부����������� ������������������  프로시저;;����������� ������������������  poly를����������� ������������������  표현하는����������� ������������������  프로시저(define����������� ������������������  (make-poly����������� ������������������  variable����������� ������������������  term-list)����������� ������������������  ����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (cons����������� ������������������  variable����������� ������������������  term-list))����������� ������������������  ����������� ������������������  

(define����������� ������������������  (variable����������� ������������������  p)����������� ������������������  (car����������� ������������������  p))����������� ������������������  ����������� ������������������  

(define����������� ������������������  (term-list����������� ������������������  p)����������� ������������������  (cdr����������� ������������������  p))����������� ������������������  ����������� ������������������  

;;����������� ������������������  마디와����������� ������������������  리스트����������� ������������������  표현����������� ������������������  방식����������� ������������������  

〈procedures����������� ������������������  adjoin-term����������� ������������������  ...����������� ������������������  coeff����������� ������������������  from����������� ������������������  text����������� ������������������  below〉����������� ������������������  ����������� ������������������  

(define����������� ������������������  (add-poly����������� ������������������  p1����������� ������������������  p2)����������� ������������������  ...)����������� ������������������  ����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  〈procedures����������� ������������������  used����������� ������������������  by����������� ������������������  add-poly〉����������� ������������������  ����������� ������������������  

(define����������� ������������������  (mul-poly����������� ������������������  p1����������� ������������������  p2)����������� ������������������  ...)����������� ������������������  ����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  〈procedures����������� ������������������  used����������� ������������������  by����������� ������������������  mul-poly〉����������� ������������������  

;;����������� ������������������  이����������� ������������������  패키지를����������� ������������������  등록하는����������� ������������������  코드����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (define����������� ������������������  (tag����������� ������������������  p)����������� ������������������  (attach-tag����������� ������������������  ’polynomial����������� ������������������  p))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (put����������� ������������������  ’add����������� ������������������  ’(polynomial����������� ������������������  polynomial)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (lambda����������� ������������������  (p1����������� ������������������  p2)����������� ������������������  (tag����������� ������������������  (add-poly����������� ������������������  p1����������� ������������������  p2))))����������� ������������������  (put����������� ������������������  ’mul����������� ������������������  ’(polynomial����������� ������������������  polynomial)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (lambda����������� ������������������  (p1����������� ������������������  p2)����������� ������������������  (tag����������� ������������������  (mul-poly����������� ������������������  p1����������� ������������������  p2))))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (put����������� ������������������  ’make����������� ������������������  ’polynomial����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (lambda����������� ������������������  (var����������� ������������������  terms)����������� ������������������  (tag����������� ������������������  (make-poly����������� ������������������  var����������� ������������������  terms))))����������� ������������������  ’done)����������� ������������������  

인터페이스����������� ������������������  정의

Page 21: SICP_2.5 일반화된 연산시스템

(define����������� ������������������  (add-poly����������� ������������������  p1����������� ������������������  p2)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (if����������� ������������������  (same-variable?����������� ������������������  (variable����������� ������������������  p1)����������� ������������������  (variable����������� ������������������  p2))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (make-poly����������� ������������������  (variable����������� ������������������  p1)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (add-terms����������� ������������������  (term-list����������� ������������������  p1)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (term-list����������� ������������������  p2)))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (error����������� ������������������  "Polys����������� ������������������  not����������� ������������������  in����������� ������������������  same����������� ������������������  var����������� ������������������  -����������� ������������������  ADD-POLY"����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (list����������� ������������������  p1����������� ������������������  p2))))

다항식의����������� ������������������  덧셈(define����������� ������������������  (add-terms����������� ������������������  L1����������� ������������������  L2)����������� ������������������  

(cond����������� ������������������  ((empty-termlist?����������� ������������������  L1)����������� ������������������  L2)����������� ������������������  ((empty-termlist?����������� ������������������  L2)����������� ������������������  L1)����������� ������������������  

(else����������� ������������������  (let����������� ������������������  ((t1����������� ������������������  (first-term����������� ������������������  L1))����������� ������������������  (t2����������� ������������������  (first-term����������� ������������������  L2)))����������� ������������������  

(cond����������� ������������������  ((>����������� ������������������  (order����������� ������������������  t1)����������� ������������������  (order����������� ������������������  t2))����������� ������������������  ����������� ������������������  ;����������� ������������������  차수����������� ������������������  비교����������� ������������������  (adjoin-term����������� ������������������  t1����������� ������������������  (add-terms����������� ������������������  (rest-terms����������� ������������������  L1)����������� ������������������  L2)))����������� ������������������  

((<����������� ������������������  (order����������� ������������������  t1)����������� ������������������  (order����������� ������������������  t2))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (adjoin-term����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  t2����������� ������������������  (add-terms����������� ������������������  L1����������� ������������������  (rest-terms����������� ������������������  L2))))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (else����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (adjoin-term����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (make-term����������� ������������������  (order����������� ������������������  t1)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (add����������� ������������������  (coeff����������� ������������������  t1)����������� ������������������  (coeff����������� ������������������  t2)))����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (add-terms����������� ������������������  (rest-terms����������� ������������������  L1)����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (rest-terms����������� ������������������  L2)))))))))

Page 22: SICP_2.5 일반화된 연산시스템

����������� ������������������  ����������� ������������������  ����������� ������������������  (define����������� ������������������  (mul-poly����������� ������������������  p1����������� ������������������  p2)����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (if����������� ������������������  (same-variable?����������� ������������������  (variable����������� ������������������  p1)����������� ������������������  (variable����������� ������������������  p2))����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (make-poly����������� ������������������  (variable����������� ������������������  p1)����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (mul-terms����������� ������������������  (term-list����������� ������������������  p1)����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (term-list����������� ������������������  p2)))����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (error����������� ������������������  "Polys����������� ������������������  not����������� ������������������  in����������� ������������������  same����������� ������������������  var����������� ������������������  -����������� ������������������  MUL-POLY"����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (list����������� ������������������  p1����������� ������������������  p2))))

다항식의����������� ������������������  곱셈

(define����������� ������������������  (mul-terms����������� ������������������  L1����������� ������������������  L2)����������� ������������������  

(if����������� ������������������  (empty-termlist?����������� ������������������  L1)����������� ������������������  (the-empty-termlist)����������� ������������������  

(add-terms����������� ������������������  (mul-term-by-all-terms����������� ������������������  (first-term����������� ������������������  L1)����������� ������������������  L2)����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  (mul-terms����������� ������������������  (rest-terms����������� ������������������  L1)����������� ������������������  L2))))����������� ������������������  

;;����������� ������������������  하나의����������� ������������������  항을����������� ������������������  다항식에����������� ������������������  곱하는����������� ������������������  프로시저����������� ������������������  

(define����������� ������������������  (mul-term-by-all-terms����������� ������������������  t1����������� ������������������  L)����������� ������������������  

(if����������� ������������������  (empty-termlist?����������� ������������������  L)����������� ������������������  (the-empty-termlist)����������� ������������������  

(let����������� ������������������  ((t2����������� ������������������  (first-term����������� ������������������  L)))����������� ������������������  

(adjoin-term����������� ������������������  

(make-term����������� ������������������  (+����������� ������������������  (order����������� ������������������  t1)����������� ������������������  (order����������� ������������������  t2))����������� ������������������  

(mul����������� ������������������  (coeff����������� ������������������  t1)����������� ������������������  (coeff����������� ������������������  t2)))����������� ������������������  

(mul-term-by-all-terms����������� ������������������  t1����������� ������������������  (rest-terms����������� ������������������  L))))))

Page 23: SICP_2.5 일반화된 연산시스템

기호식����������� ������������������  대수에서����������� ������������������  데이터����������� ������������������  타입����������� ������������������  사이의����������� ������������������  계층����������� ������������������  관계

요약된����������� ������������������  되돌이����������� ������������������  데이터(recursive����������� ������������������  data����������� ������������������  abstraction)����������� ������������������  를����������� ������������������  통해����������� ������������������  

다항식이����������� ������������������  다시����������� ������������������  다항식으로����������� ������������������  구성될����������� ������������������  수����������� ������������������  있는����������� ������������������  구조를����������� ������������������  갖춤����������� ������������������  

일반화된����������� ������������������  연산과����������� ������������������  데이터����������� ������������������  중심����������� ������������������  프로그램����������� ������������������  방식으로����������� ������������������  복잡한����������� ������������������  문제를����������� ������������������  해결����������� ������������������  

다항식����������� ������������������  대수����������� ������������������  시스템의����������� ������������������  경우����������� ������������������  타입����������� ������������������  사이의����������� ������������������  상관관계를����������� ������������������  Tower로����������� ������������������  쌓아����������� ������������������  올리지����������� ������������������  못함����������� ������������������  

(여러개의����������� ������������������  변수로����������� ������������������  구성된����������� ������������������  다항식)����������� ������������������  

이러한����������� ������������������  경우,����������� ������������������  한����������� ������������������  다항식을����������� ������������������  항들을����������� ������������������  펼쳐����������� ������������������  다시����������� ������������������  정돈����������� ������������������  하면서����������� ������������������  두����������� ������������������  다항식의����������� ������������������  

주된����������� ������������������  변수를����������� ������������������  동일하도록����������� ������������������  만든����������� ������������������  후����������� ������������������  연산����������� ������������������  (Normal����������� ������������������  형태로����������� ������������������  변경)

Page 24: SICP_2.5 일반화된 연산시스템

Q&A

Page 25: SICP_2.5 일반화된 연산시스템

Reference• Harold����������� ������������������  Abelson,����������� ������������������  Gerald����������� ������������������  Jay����������� ������������������  Sussman,����������� ������������������  Julie����������� ������������������  Sussman,����������� ������������������  컴퓨

터����������� ������������������  프로그램의����������� ������������������  구조와����������� ������������������  해석(김재우,����������� ������������������  안윤호,����������� ������������������  김수정,����������� ������������������  김정민����������� ������������������  옮김).����������� ������������������  서울시����������� ������������������  마포구:����������� ������������������  인사이트,����������� ������������������  2008