1 / 10

구조가 있는 값 만들기

구조가 있는 값 만들기. 지금까지 만들 수 있었던 값의 종류 기초타입의 값들 : int, bool, string 함수타입의 값들 : t->t 복합적인 것들을 어떻게 만들지 ? 만든 값들을 가지고 조립해서 만들고 싶다 ! 짝 / 튜플 tuple: int*int, string*int*(int->bool). 구조물 만들기 + 구조물 사용하기. <expr> ::= ... | (cons <expr> <expr>) | (car <expr>) | (cdr <expr>)

joben
Télécharger la présentation

구조가 있는 값 만들기

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. 구조가 있는 값 만들기 • 지금까지 만들 수 있었던 값의 종류 • 기초타입의 값들: int, bool, string • 함수타입의 값들: t->t • 복합적인 것들을 어떻게 만들지? • 만든 값들을 가지고 조립해서 만들고 싶다! • 짝/튜플tuple: int*int, string*int*(int->bool)

  2. 구조물 만들기 + 구조물 사용하기 <expr> ::= ... | (cons <expr> <expr>) | (car <expr>) | (cdr <expr>) | (null? <expr>) | ()

  3. 1 2 • (cons 1 2) • (cons 1 (cons 2 ())) • (car <expr>) (cdr <expr>) 1 2 cdr car

  4. Types introduction for (t1 t2) • cons : t1 * t2 -> (t1 t2) • car : (t1 t2) -> t1 • cdr : (t1 t2) -> t2 • (): nil (cons 1 #t) : (int bool) (cons 1 2) : (int int) (car (cons 1 2)) : int (cdr (cons 1 2)) : int (cons 1 ()) : (int nil) (cons 2 (cons 1 ())): (int (int nil)) elimination for (t1 t2)

  5. list: sugar for nested cons • (list) = () • (list 1) = (cons 1 ()) • (list 1 2) = (cons 1 (cons 2 ())) • (list <e1> <e2> … <eN>) = (cons <e1> (cons <e2> … (cons <eN> ())…)

  6. ‘<exp>: sugar for (quote <exp>) • (quote <exp>) does not evaluate <exp>; return <exp> as it is • ‘<exp> = (quote <exp>) • ‘<name> evaluates to <name>, not its value • ‘(1 2 3) = (quote (1 2 3)) = (list ‘1 ‘2 ‘3) • ‘(x y z) = (quote (x y z)) = (list ‘x ‘y ‘z) • ‘(<e1> … <eN>) = (quote (<e1>…<eN>)) = (list ‘<e1> …‘<eN>)

  7. Types • int-list = int가 0개 이상 있는 리스트 = nil | (int nil) | (int (int nil)) | … = nil | (int int-list) • int-list를 만드는 방법 2가지: • nil 값 • int 값을 int-list 값의 앞에 붙인것 • () : t-list • cons : t * t-list -> t-list • car : t-list -> t • cdr : t-list -> t-list • null? : t-list -> bool (cons 1 ()) : int-list (cons 2 (cons 1 ())): int-list

  8. Types • 타입t+t’ list = t 혹은 t’원소가 0개 이상 있는 리스트 = nil | (t+t’ nil) | (t+t’ (t+t’ nil)) | … = nil | (t+t’ (t+t’ list)) ‘(1 #t) : int+bool list • nil : t+t’ list • cons : (t+t’) * (t+t’ list) -> t+t’ list • car : t+t’ list -> t+t’ • cdr : t+t’ list -> t+t’ list • null? : t+t’ list -> bool (cons “a” (cons 1 nil)) : (string+int) list (car (cons “a” (cons 1 nil))) : string

  9. Typeful programming (define (aging animal) (define (add-age x) (cond ((is-name? x) (string-append x “님”)) ((is-integer? x) (+ x 1)) )) (if (null? animal) () (cons (add-age (car animal)) (aging (cdr animal)) ) ) ) case analysis on types

  10. Type-safe programming (define (aging animal) (define (add-age x) (cond ((is-name? x) (string-append x “님”)) ((is-integer? x) (+ x 1)) )) (if (null? animal) () …)) • 위의 aging함수가 불릴때 전달되는 animal list는 항상 이름 또는 정수만 원소로 가지고 있는가? • Your program is type-safe. • 혹시 그렇지 않다면? 위의 두 테스트 케이스는 충분하지 않다면? 잘 모르겠으면? • Your program is not type-safe. • 따라서, 프로그램은 실행중에 갑자기 멈출수 있다. • 어쩔 것인가? 해결할 방법이 무엇일까?

More Related