CS220 Programming Principles

48
CS220 Programming Principles 프프프프프프 프프 2002 프프프프 Class 5 프프

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

Page 1: CS220 Programming Principles

CS220Programming Principles

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

Page 2: CS220 Programming Principles

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

Page 3: CS220 Programming Principles

3

Three Description of Pairs

Input Expression (cons 1 2) Box and Pointer

Output Representation ( 1 . 2) ; dotted pair notation

1 2

Page 4: CS220 Programming Principles

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.

Page 5: CS220 Programming Principles

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

+

Page 6: CS220 Programming Principles

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

Page 7: CS220 Programming Principles

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.

/

Page 8: CS220 Programming Principles

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

Page 9: CS220 Programming Principles

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)

Page 10: CS220 Programming Principles

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

Page 11: CS220 Programming Principles

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)

Page 12: CS220 Programming Principles

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

Page 13: CS220 Programming Principles

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

Page 14: CS220 Programming Principles

14

List Operations(V)

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

Page 15: CS220 Programming Principles

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

Page 16: CS220 Programming Principles

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

Page 17: CS220 Programming Principles

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)

Page 18: CS220 Programming Principles

18

List Operations(VIII)

Accumulations

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

Page 19: CS220 Programming Principles

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)

Page 20: CS220 Programming Principles

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

Page 21: CS220 Programming Principles

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

Page 22: CS220 Programming Principles

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

Page 23: CS220 Programming Principles

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

Page 24: CS220 Programming Principles

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

Page 25: CS220 Programming Principles

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

Page 26: CS220 Programming Principles

26

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

3 4

1 2

Page 27: CS220 Programming Principles

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

Page 28: CS220 Programming Principles

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)

Page 29: CS220 Programming Principles

29

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

3 4

1

2

1

2

3 4

Page 30: CS220 Programming Principles

30

Ex 2.25 - pick 7

(1 2 (5 7) 9)

((7))

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

Page 31: CS220 Programming Principles

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

Page 32: CS220 Programming Principles

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

Page 33: CS220 Programming Principles

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

+

Page 34: CS220 Programming Principles

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”

Page 35: CS220 Programming Principles

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

Page 36: CS220 Programming Principles

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

Page 37: CS220 Programming Principles

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

Page 38: CS220 Programming Principles

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

Page 39: CS220 Programming Principles

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

Page 40: CS220 Programming Principles

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

Page 41: CS220 Programming Principles

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

Page 42: CS220 Programming Principles

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

Page 43: CS220 Programming Principles

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

Page 44: CS220 Programming Principles

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

Page 45: CS220 Programming Principles

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

Page 46: CS220 Programming Principles

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)

Page 47: CS220 Programming Principles

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

Page 48: CS220 Programming Principles

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