vs define append L R cond null L

  • Slides: 14
Download presentation

값 중심 vs 물건 중심 프로그래밍 • 값중심 (define (append L R) (cond ((null?

값 중심 vs 물건 중심 프로그래밍 • 값중심 (define (append L R) (cond ((null? L) R) ((null? R) L) (else (cons (car L) (append (cdr L) R))) )) • 물건중심: “L을 바꿔서 L+R이되게” (define (append L R) (cond ((null? L) (make-L R; L)) ((null? R) L) (else (make-L’s cdr (append (cdr L) R); L)) ))

바꿔!연산자들 mutators (2/3) • (set-car! <exp 1> <exp 2>) <exp 1> 이 cons-cell이어야 한다

바꿔!연산자들 mutators (2/3) • (set-car! <exp 1> <exp 2>) <exp 1> 이 cons-cell이어야 한다 <exp 1>의 car part가 <exp 2>로 바뀐다. • (define L ‘(1 2)) (set-car! L “kwang”) L ==> (“kwang” 2) (set-car! (cdr L) “teaches 102”) L ==> (“kwang” “teaches 102”) (set-car! (cdr L) ‘(“teaches” “ 102”)) L ==> (“kwang” (“teaches” “ 102”))

바꿔!연산자들 mutators (3/3) • (set-cdr! <exp 1> <exp 2>) <exp 1> 이 cons-cell이어야 한다

바꿔!연산자들 mutators (3/3) • (set-cdr! <exp 1> <exp 2>) <exp 1> 이 cons-cell이어야 한다 <exp 1>의 cdr part가 <exp 2>로 바뀐다. • (define L ‘(1 2)) (set-cdr! L ‘(“kwang”)) L ==> (1 “kwang”) (set-cdr! (cdr L) “teaches 102”) L ==> (1 “kwang”. “teaches 102”) (set-cdr! (cdr L) ‘(“teaches” “ 102”)) L ==> (1 “kwang” “teaches” “ 102”)

바꿔!연습 1 (define x (define y (set-car! x ==> ((3 ‘(1 2)) ‘(3 4))

바꿔!연습 1 (define x (define y (set-car! x ==> ((3 ‘(1 2)) ‘(3 4)) x y) 4) 2) followed by (set-cdr! x y) x ==> ((3 4)

바꿔!연습 2 (define x (define y (set-car! x ==> ((3 ‘(1 2)) ‘(3 4))

바꿔!연습 2 (define x (define y (set-car! x ==> ((3 ‘(1 2)) ‘(3 4)) x y) 4) 2) followed by (set-car! y x) x ==> (((…()…) 4) 2) ; has an abyss

바꿔!연습 3 (define x ‘(1)) (define y ‘(2)) (set-cdr! x y) x ==> (1

바꿔!연습 3 (define x ‘(1)) (define y ‘(2)) (set-cdr! x y) x ==> (1 2) followed by (set-cdr! y x) x ==> (1 2 1 2 …) infinite list, a cycle

데이타구현: 값중심 vs 물건중심 예) 스택 stack empty: stk push: stk * elmt ->

데이타구현: 값중심 vs 물건중심 예) 스택 stack empty: stk push: stk * elmt -> void push: stk * elmt -> stk is-empty? : stk -> bool pop: stk -> elmt * stk (define empty ()) (define (push s x) (cons x s)) (define is-empty? null? ) (define (pop s) (if (is-empty? s) (error) s) (define empty (cons nil)) (define (push s x) (let ((cell (cons x nil))) (set-cdr! cell (cdr s)) (set-cdr! s cell))) (define (is-empty? s) (null? (cdr s))) (define (pop s) (if (is-empty? s) (error) …))

바꿔!연산자를 이용한 물건중심구현 (1/2) • 복잡하다: 프로그램짜기 어렵다 – 왜? 물건의 상태가 변하는 것을

바꿔!연산자를 이용한 물건중심구현 (1/2) • 복잡하다: 프로그램짜기 어렵다 – 왜? 물건의 상태가 변하는 것을 추적하면서 프로그램해야. – 왜? 물건이 어떻게 메모리에 구현되는지를 알아야: pointers, pointers… – 왜? 예전엔 미처 생각못한 일이 벌어질 수 있다. ; ; db is a list. (define (last-knowledge db) (cond ((null? db) ()) ((null? (cdr db)) (car db)) (else (last-knowledge (cdr db))))). . . ; ; happy programming, never imagine that db can be a cycle. . . ; ; after 2 months, 30 K lines of pgming, at a gloomy night (set-cdr! db bd) (set-cdr! bd db) ; ; I need a cycle. . . ; ; then this will kill me (last-knowledge db) ; ; this never returns!

물건중심의 프로그래밍 예: function-dispatch table ‘real ‘rectangular real-r ‘polar real-p ‘xyz real-x ‘imag-r imag-p

물건중심의 프로그래밍 예: function-dispatch table ‘real ‘rectangular real-r ‘polar real-p ‘xyz real-x ‘imag-r imag-p imag-x ‘mag ‘angle mag-r angle-r mag-p angle-p mag-x angle-x (define (real c) ((dispatch (type c) ‘real) c))) (define (angle c) ((dispatch (type c) ‘angle) c)) (define (imag c) ((dispatch (type c) ‘imag) c)) (define (mag c) ((dispatch (type c) ‘mag) c))

층층이 속내용 감추기 Abstraction Hierarchy • attach tags to data about types and reprsntns

층층이 속내용 감추기 Abstraction Hierarchy • attach tags to data about types and reprsntns • use dispatch tables for improved additivity add-rational mul-rational denom numer is-rational? … mul add-complex mul-complex add-intvl mul-intvl real imag angle is-complex? … min max is-ntvl? … real-r imag-r … real-p iamg-p… pair representation rectangular polar representation pair representation