List Data

Click here to load reader

download List Data

of 15

description

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) … ) 타고 내려가며 만들어 올리기 - PowerPoint PPT Presentation

Transcript of List Data

  • List Datalist () : t-listcons : t * t-list -> t-listlist car : t-list -> t cdr : t-list -> t-listnull? : t-list -> bool

  • List Programming Patterns (define (build from to) ) (define (length lst) ) (define (append lst1 lst2) ) (define (accumulate lst) )(define (filter p lst) )

  • Programming On Structured Data :) (1 2 3 4): nil,cons,cons,cons,cons

  • List Data Abstractionlist (how) . .list nil : t-listlink : t * t-list -> t-listlist first : t-list -> trest : t-list -> t-listempty? : t-list -> bool

  • Data Abstraction Conceptnil, link, first, rest, empty?programs that use listimplementation 1implementation 2

  • List Programming Patterns (define (build from to) ) (define (length lst) ) (define (append lst1 lst2) ) (define (accumulate lst) )(define (filter p lst) )

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

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

  • 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?)

  • 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 . .

  • Data Abstraction ? (first (cons 1 (link 2 nil))) t-list cons t-list .t-list cons , . (abstraction barrier) . ? ? ?

  • Data Abstraction Example 2: Rational Number Datarational number make-rat: int * int -> ratrational number numer: rat -> intdenom: rat -> int

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

  • Rational Number Operations:abstraction layer 2add-rat: rat * rat -> ratsub-rat: rat * rat -> ratmul-rat: rat * rat -> ratdiv-rat: rat * rat -> ratequal-rat?: rat * rat -> bool

    Define them using only the procedures in the abstraction layer 1: make-rat, numer, and denom

  • Abstraction Hierarchy(), cons, car, cdrmake-rat, numer, denomadd-rat, sub-rat, mul-rat, div-rat, equal-rat?programs that use rational numbers