480 likes | 629 Views
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 <v 1 > <V 2 >)) = <V 1 > (cdr (cons <v 1 > <V 2 >)) = <V 2 > (pair? <p>) => #t if <p> is a pair
E N D
CS220Programming Principles 프로그래밍의 이해 2002 가을학기 Class 5 한 태숙
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
Three Description of Pairs • Input Expression (cons 1 2) • Box and Pointer • Output Representation ( 1 . 2) ; dotted pair notation 1 2
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.
Definition of List • 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)) +
2 3 4 5 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))))
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. /
2 5 3 4 Lists : examples (list 2 (list 3 4) 5) => (2 (3 4) 5) (cons 2 (cons (cons 3 (cons 4 nil)) (cons 5 nil)))
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)
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))))
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)
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))))
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))))
List Operations(V) • Transforming a list (define (square-list items) (if (null? items) empty (cons (square (car items)) (square-list (cdr items)))))
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))
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))
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)
List Operations(VIII) • Accumulations (define (add-up items) (if (null? items) 0 (+ (car items) (add-up (cdr items)))))
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)
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)->S accumulate:((T,S)->S,S,List(T))->S
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)))
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))))
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)))))
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))
Ex 2.23 for-each (for-each (lambda (x) (newline) (display x)) (list 57 321 88)) 57 321 88 (define (for-each f data) (if (null? data) #t ; arbitrary value ;(newline) is OK. (begin (f (car data)) (for-each f (cdr data)))))
3 4 1 2 Hierarchical Structures • Tree - a sequence of sequences (cons (list 1 2) (list 3 4))=>((1 2) 3 4)
((1 2) 3 4) (1 2) 3 4 1 2 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
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)
3 4 2 Ex. 2.24 (list 1 (list 2 (list 3 4))) => (1 (2 (3 4))) 1 2 1 3 4
Ex 2.25 - pick 7 (1 2 (5 7) 9) ((7)) (1 (2 (3 (4 (5 (6 7))))))
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) ))))
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))
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 +
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”
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)))
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))))
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))
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))
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))))
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))
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))))
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) >
Solution hard-to-easy enumerate: integers filter: even? (define (easy lo hi) (accumulate * 1 (map fib (filter even? enumerate-interval lo hi))))) accumulate: * 1 map: fib
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))))))
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
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)
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))
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)))