1 / 15

List Data

List Data. list 만드는 방법 () : t-list cons : t * t-list -> t-list list 사용하는 방법 car : t-list -> t cdr : t-list -> t-list null? : t-list -> bool. List Programming Patterns. 만들어 올리기 (define (build from to) … ) 타고 내려가기 (define (length lst) … ) 타고 내려가며 만들어 올리기

breck
Télécharger la présentation

List Data

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

E N D

Presentation Transcript


  1. List Data • list 만드는 방법 • () : t-list • cons : t * t-list -> t-list • list 사용하는 방법 • car : t-list -> t • cdr : t-list -> t-list • null? : t-list -> bool

  2. List Programming Patterns • 만들어 올리기 • (define (build from to) …) • 타고 내려가기 • (define (length lst) …) • 타고 내려가며 만들어 올리기 • (define (append lst1 lst2) …) • 접어 모으기 • (define (accumulate lst) …) • 걸러내기 • (define (filter p lst) …)

  3. Programming On Structured Data • 데이터의 구조는 데이터를 만드는 방법들의 조합으로 결정된다: • 예)리스트(1 2 3 4)는: nil,cons,cons,cons,cons로 구성 • 데이터를 다루는 프로그램은 데이터의 구조를 따라서 일을 한다

  4. List Data Abstraction • list를 만들고 사용하는 방법(how)이 무엇인지 이름으로 감춘다. • 외부에서는 그 이름만 사용해서 프로그램한다. • list 만드는 방법 • nil : t-list • link : t * t-list -> t-list • list 사용하는 방법 • first : t-list -> t • rest : t-list -> t-list • empty? : t-list -> bool

  5. Data Abstraction Concept programs that use list nil, link, first, rest, empty? implementation 2 implementation 1

  6. List Programming Patterns • 만들어 올리기 • (define (build from to) …) • 타고 내려가기 • (define (length lst) …) • 타고 내려가며 만들어 올리기 • (define (append lst1 lst2) …) • 접어 모으기 • (define (accumulate lst) …) • 걸러내기 • (define (filter p lst) …)

  7. Implementation of List Data (1) (define nil ()) (define link cons) (define first car) (define rest cdr) (define empty? null?)

  8. Implementation of List Data (2) (define nil ()) (define (link x lst) (lambda (f) (f x lst))) (define (first lst) (lst (lambda (x y) x))) (define (rest lst) (lst (lambda (x y) y))) (define empty? null?) (first (link 1 nil)) (first (rest (link 1 (link 2 nil))))

  9. Implementation of List Data (3) (define nil ()) (define (link x lst) (define (I-am-cons m) (cond ((equal? m ‘f) x) ((equal? m ‘r) lst) (else (error “unknown message”)) )) I-am-cons) (define (first lst) (lst ‘f)) (define (rest lst) (lst ‘r)) (define empty? null?)

  10. Data Abstraction이 좋은점 • 데이터를 만들고 사용하는 사람은 아래 다섯개만 사용해서 프로그램한다. t-list가 어떻게 구현되었는지 알바 없다. • nil: t-list • link: t * t-list -> t-list • first: t-list -> t • rest: t-list -> t-list • empty?: t-list -> bool • 구현된 속내용을 바꾸어도 프로그램의 다른 부분은 상관없다. • 구현된 속내용을 바꾸고 싶은 경우 위의 다섯개만 바꾸면 된다.

  11. Data Abstraction의 좋은점이 깨질 수 있는 여지 • 혹시 프로그래머가 다음과 같이 프로그램하면? • … (first (cons 1 (link 2 nil)))… • t-list 가 cons로 구현되었다면 잘 돌고 • t-list 가 다른방식으로 구현되었다면 안 돈다. • t-list 를 cons에서 새로운 방법으로 구현하게 되면, 잘 돌던 것이 갑자기 돌지않게된다. • 프로그래머가 그렇게 “요약의 경계”(abstraction barrier)를 넘나들게 하는 것은 좋지않다. • 그런 프로그램이 짜지지 않게 하려면 어떻게 해야 할까? • 그런 프로그램인지 아닌지 자동으로 확인해 주는 도구가 있다면? 필요하지 않을까?

  12. Data Abstraction Example 2: Rational Number Data • rational number 만드는 방법 • make-rat: int * int -> rat • rational number 사용하는 방법 • numer: rat -> int • denom: rat -> int

  13. Rational Number Implementations (define (make-rat a b) (cons a b)) (define (numer r) (car r)) (define (denom r) (cdr r)) (define (make-rat a b) (cons b a)) (define (numer r) (cdr r)) (define (denom r) (car r)) (define (make-rat a b) (cons (encrypt a) (encrypt b))) (define (numer r) (decrypt (car r)) (define (denom r) (decrypt (cdr r))

  14. Rational Number Operations:abstraction layer 2 • add-rat: rat * rat -> rat • sub-rat: rat * rat -> rat • mul-rat: rat * rat -> rat • div-rat: rat * rat -> rat • equal-rat?: rat * rat -> bool Define them using only the procedures in the abstraction layer 1: make-rat, numer, and denom

  15. Abstraction Hierarchy programs that use rational numbers add-rat, sub-rat, mul-rat, div-rat, equal-rat? make-rat, numer, denom (), cons, car, cdr

More Related