CS220 Programming Principles

Post on 19-Mar-2016

29 views 0 download

description

CS220 Programming Principles. 프로그래밍의 이해 2002 가을학기 Class 5 한 태숙. Remind pairs!. cons : T 1 , T 2 -> T 1 x T 2 car : T 1 x T 2 -> T 1 cdr : T 1 x T 2 -> T 2 (car (cons )) = (cdr (cons )) = (pair? ) => #t if is a pair - PowerPoint PPT Presentation

Transcript of CS220 Programming Principles

CS220Programming Principles

프로그래밍의 이해2002 가을학기 Class 5한 태숙

2

Remind pairs!

cons : T1 , T2 -> T1 x T2

car : T1 x T2 -> T1

cdr : T1 x T2 -> T2

(car (cons <v1> <V2>)) = <V1>(cdr (cons <v1> <V2>)) = <V2>(pair? <p>) => #t if <p> is a pair #f otherwise

3

Three Description of Pairs

Input Expression (cons 1 2) Box and Pointer

Output Representation ( 1 . 2) ; dotted pair notation

1 2

4

List List?

– A series of cons cells whose cdrs points to the next cell

– Last element has a cdr pointing to the empty list

– the value “nil” is usually the empty list (In DrScheme, it’s “empty”.)

– Lists are closed under the operations of cons and cdr.

5

The “empty list”, nil, is a list of type T, for any T

If <L> is a list (of element of) type T, and <v> is a value of type T, then (cons <v> L) is a list (of element of) type T.

Type Equation List(T) = {nil} U (T x List(T))

Definition of List

+

6

list

List always conses exactly as many cells as there are arguments to list.

(list 2 3 4 5) <=> (cons 2 (cons 3 (cons 4 (cons 5 nil))))

2 3 4 5

7

List Contract

(null? nil) => #t(null? <v>) => #f if <v> = nil

(car (cons <v> <L>)) => <v>(cdr (cons <v> <L>)) => <L>

Non-null Lists are a special case of Pairs.

/

8

Lists : examples

(list 2 (list 3 4) 5) => (2 (3 4) 5)(cons 2 (cons (cons 3 (cons 4 nil)) (cons 5 nil)))

2

3 4

5

9

Cars and Cdrs of lists

(define L (list 2 (list 3 4) 5))

(car L) =>(car (cdr L) => =(cadr L)(car (cadr L) =>(cdr (cadr L) => =(cdadr L)(caddr L) =>(cdddr L) => (cons 10 L) =>

(2 (3 4) 5)

10

List Operations(I)

cdring down a list(define (list-ref items n) (if (= n 0) (car items) (list-ref (cdr items) (- n 1)))))(define (length items) (if (null? items) 0 (+ 1 (length (cdr items))))

11

List Operations(II)

Consing up a list(define (enumerate-interval from to) (if (> from to) empty (cons from (enumerate-interval (+ from 1) to))))Ex: (enumerate-interval 2 10) => (2 3 4 5 6 7 8 9 10)

12

List Operations(III)

Cdr down the list and cons up a result

(define (copy items) (if (null? items) empty (cons (car items) (copy (cdr items))))

13

List Operations(IV)

Cdr down the list and cons up a result

(define (append list1 list2) (if (null? list1) list2 (cons (car list1) (append (cdr list1) list2))))

14

List Operations(V)

Transforming a list(define (square-list items) (if (null? items) empty (cons (square (car items)) (square-list (cdr items)))))

15

List Operations(VI) Mapping over lists(define (map proc items) (if (null? items) empty (cons (proc (car items)) (map proc (cdr items)))))

example: (define (square-list items) (map square items))

16

Map of Scheme

(map <f> (list <v0> <v1> …<vn>))=> (list (<f> <v0>)(<f> <v1>)… (<f> <vn>))Ex: (map list (list 1 2 3 4)) => ((1) (2) (3) (4))(map <f> list1 list2) =? ???(map list (list 1 2 3)(list 4 5 6))=> ((1 4) (2 5) (3 6))

17

List Operations(VII)

Filtering(define (filter pred items) (cond ((null? items) empty) ((pred (car items)) (cons (car items) (filter pred (cdr items)))) (else (filter pred (cdr items)))))Ex: (filter odd? (list 1 2 3 4 5 6)) => (1 3 5)

18

List Operations(VIII)

Accumulations

(define (add-up items) (if (null? items) 0 (+ (car items) (add-up (cdr items)))))

19

List Operations(IX) Generalized accumulation(define (accumulate op init items) (if (null? items) init (op (car items) (accumulate op init (cdr items)))))example: (define (add-up items) (accumulate + 0 items)

20

Type of Accumulate

Ex: (accumulate + 0 (list 1 2 5 6)) => (+ 1 (+ 2 (+ 5 (+ 6 0)))) =>14

items : List(T) init : S op :(T,S)->Saccumulate:((T,S)->S,S,List(T))->S

21

Examples

Ex: (map fib (enumerate-interval 10 20)))Ex: (filter even? (map fib (enumerate-inteval 10 20)))Ex: (map fib (filter even? (enumerate-interval 10 20)))

22

Exercise 2.17 last-pair

(last-pair (list 23 72 149 34))

(34)

(define (last-pair lst) (if (null? (cdr lst)) lst (last-pair (cdr lst))))

23

Examples

Ex 2.18 define a procedure reverse(reverse (list 1 2 3)) => (3 2 1)(reverse ()) => ()

(define (reverse lst) (if (null? lst) empty (append (reverse (cdr lst)) (list (car lst)))))

24

Iterative version of reverse(define (reverse lst) (if (null? lst) empty (append (reverse (cdr lst)) (list (car list)))))

(define (reverse lst) (define (rev input result) (if (null? input) result (rev (cdr input) (cons (car input) result)))) (rev lst empty))

25

Ex 2.23 for-each(for-each (lambda (x) (newline) (display x)) (list 57 321 88))

5732188

(define (for-each f data) (if (null? data) #t ; arbitrary value ;(newline) is OK. (begin (f (car data)) (for-each f (cdr data)))))

26

Hierarchical Structures Tree - a sequence of sequences(cons (list 1 2) (list 3 4))=>((1 2) 3 4)

3 4

1 2

27

Tree

(define x (list (list 1 2) 3 4))(list x x)(((1 2) 3 4)((1 2) 3 4))

(length x) 3(count-leaves x)4

((1 2) 3 4)

(1 2)3 4

1 2

28

Count-leaves

(define (count-leaves x) (cond ((null? x) 0) ((not (pair? x)) 1) (else (+ (count-leaves (car x)) (count-leaves (cdr x)) ))))

cf: (length items)

29

Ex. 2.24 (list 1 (list 2 (list 3 4)))=> (1 (2 (3 4)))

3 4

1

2

1

2

3 4

30

Ex 2.25 - pick 7

(1 2 (5 7) 9)

((7))

(1 (2 (3 (4 (5 (6 7))))))

31

Mapping over trees(scale-tree (list 1 (2 (3 4) 5) (6 7)) 10)=> (10 (20 (30 40) 50) (60 70))

(define (scale-tree tree factor) (cond ((null? tree) empty) ((not (pair? tree))(* tree factor)) (else (cons (scale-tree (car tree) factor) (scale-tree (cdr tree) factor) ))))

32

Scale-tree with Map

(define (scale-tree tree factor) (map (lambda (sub-tree) (if (pair? sub-tree) (scale-tree sub-tree factor) (* sub-tree factor))) tree))

33

Type of Trees

A leaf of type T is an element of type T. A Tree of type T’s is either

– a leaf of type T, or– a list of Trees of type T’s

Tree(T) = Leaf(T) U (List(Tree(T))) Leaf(T) = T

+

34

An Implementation

(define (leaf? tr) (not (pair? tr)))

(define (make-leaf el) el)(define (make-tree list-of-trees) list-of-trees)

Contract: follows from “List Contract”

35

Operations on Trees

Tree-map: apply procedure to each leaf(define test-tree ’(1 (4 7 3) ((1 3))))(tree-map square test-tree) => (1 (16 49 9) ((1 9)))

(define (tree-map proc tr) (if (leaf? tr) (proc tr) (map (lambda (subtree)

(tree-map proc subtree)) tr)))

36

Operations on Trees(define (flatten tree) (if (leaf? tree) (list tree) (accumulate append empty (map flatten tree))))(define (count-leaves tree) (if (leaf? tree) 1 (accumulate + 0 (map count-leaves tree))))

37

Exercise 2.32 Power Set(define nil '())(define (subsets s) (if (null? s) (list nil) (let ((rest (subsets (cdr s)))) (append rest (map <??????> rest)))))

(lambda (x) (cons (car s) x))

38

Conventional Interface

Capture common patterns that are inherent in the data (especially, sequence)

(define sum-cube n) (define (aux i sum) (if (>= i n) sum (aux (+ i 1) (+ sum (cube i))))) (aux 1 0))

39

Step for summing the cube

Generate the sequence from 1 to n Compute the cube of each Accumulate by adding to 0(define (sum-cube n) (accumulate + 0 (map (lambda (x) (* x x x)) (enumerate-interval 1 n))))

40

Another Example for sequence

Product of Prime numbers

(define (product-of-primes n) (define (aux i prod) (cond ((>= i n) prod) ((prime? i) (aux (+ i 1)(* prod i)) (else (aux (+ i 1) prod)))) (aux 1 1))

41

Step for Product of Primes

Generate the sequence from 1 to n filter out the primes accumulate by multiplying to 1

(define (product-of-primes n) (accumulate * 1 (filter prime? (enumerate-interval 1 n))))

42

Do it by yourself

(define (hard lo hi) (cond ((> lo hi) 1) ((even? lo)(* (fib lo) (hard (+ lo 1) hi))) (else (hard (+ lo 1) hi))))

(hard 2 4)>

43

Solution hard-to-easy

(define (easy lo hi) (accumulate * 1 (map fib (filter even? enumerate-interval lo hi)))))

enumerate:integers

filter:even?

map:fib

accumulate:* 1

44

Enumerate Tree (tree to list)

(enumerate-tree (list 1 (list 2 (list 3 4)) 5))=> (1 2 3 4 5)

(define (enumerate-tree tree) (cond ((null? tree) empty) ((not (pair? tree)) (list tree)) (else (append (enumerate-tree (car tree)) (enumerate-tree (cdr tree))))))

45

Examples (Ex. 2.33)

Write map as accumulation(define (map p seq)(accumulate (lambda (x y)( )) empty seq) Write append as an accumulation(define (append list1 list2) (accumulate cons )) Write length as an accumulation(define (length seq) (accumulate (lambda (x y)( )) 0 seq))

cons (p x) y

list2 list1

+ 1 y

46

Dotted Tail Notation Exercise 2.20(define (f x . y) <body> )(f 1 2 3 4) in <body> x bound to 1 y bound to (2 3 4)

(define (same-parity x . y)

(same-parity 1 2 3 4 5 6 7) x==> 1 y==>(2 3 4 5 6 7)

47

Program Example (I);Generating permutations(define (permutations s) (if (null? s) ; empty set (list empty) ; seq containing empty set (accumulate append empty (map (lambda (x) (map (lambda (p) (cons x p)) (permutations (remove x

s)))) s))))(define (remove item sequence) (filter (lambda (x) (not (= x item))) sequence))

48

Program Example (II)(define (divisible? x y) (= (remainder x y) 0))(define (sieve seq) (if (null? seq) empty (cons (car seq) (sieve (filter (lambda (x) (not (divisible? x (car seq)))) (cdr seq))))))(define (primes n) (sieve (enumerate-interval 2 n)))