Class 10: Abstracting Procedures
Post on 07-Dec-2014
282 Views
Preview:
DESCRIPTION
Transcript
Class 10: Abstracting Procedures
cs1120 Fall 2011David Evans14 September 2011
Plan for Today
PS2 Due NowAbstracting List ProceduresPS2: edit-distance
PS3 posted now, due next Friday (Sept 23).
You may work with a partner you choose.If you don’t have a partner arranged, you need toemail me by 3:55pm tomorrow: - Subject: PS3 Partner Either: “I want a partner for ps3.” or “I prefer to work alone on ps3 because [ <your reason> ]”
3
Warm-up: list-sum
Define a procedure, list-sum, that takes a list of numbers as input and outputs the sum of the numbers in the input list. (list-sum (list 1 2 3)) 6
(list-sum null) 0
4
list-sum
(define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p)))))
Okay, what about list-product?
5
list-product
(define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p)))))
Okay, what about list-length?
6
list-length
(define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))
7
Comparing List Procedures
(define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))
(define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p)))))
(define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p)))))
(define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))
8
Base Cases
(define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))
(define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p)))))
(define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p)))))
(define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))
9
Recursive Calls
(define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))
(define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p)))))
(define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p)))))
(define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))
What does each do with the car of the list?
Abstracted List Procedure(define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p)))))
(define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p)))))
Master of (Almost) All List Procedures
(define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p))))(define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p)))))
(define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p)))))
Master of (Almost) All List Procedures(define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p))))
(define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))
(define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))
PS2: Edit Distance
http://mbgd.genome.ad.jp/CoreAligner/
Million Bases
Defining edit-distance
(define (edit-distance s1 s2) (if (and (null? s1) (null? s2)) 0 (if (null? s1) (length s2) (if (null? s2) (length s1) …
Base cases
(define (edit-distance s1 s2) (if (or (null? s1) (null? s2)) (+ (length s1) (length s2)) (min (if (eq? (car s1) (car s2)) ; match or mutate
(edit-distance (cdr s1) (cdr s2)) (+ 1 (edit-distance (cdr s1) (cdr s2)))) (+ 1 (edit-distance s1 (cdr s2))) ; insert in s1 (+ 1 (edit-distance (cdr s1) s2))))) ; delete from s1
(define (edit-distance s1 s2) (if (or (null? s1) (null? s2)) (+ (length s1) (length s2)) (min (+ (if (eq? (car s1) (car s2) 0 1) ; match or mutate (edit-distance (cdr s1) (cdr s2)) (+ 1 (edit-distance s1 (cdr s2))) ; insert in s1 (+ 1 (edit-distance (cdr s1) s2))))) ; delete from s1
This is the smallest correct edit-distance procedure I can find…but, if you can find a smaller one that is worth a gold star bonus! (Replacing + 1 with inc or s1/s2 with s/t doesn’t count.)
Executing edit-distance a c a t c a t g c
c a t g a t a c(edit-distance )
c a t c a t g c a t g a t a c
c a t c a t g c c a t g a t a c
a c a t c a t g c a t g a t a c
c a t c a t g c a t g a t a c
> (require racket/trace)> (trace edit-distance)> (edit-distance (list 1 2) (list 2))>(edit-distance '(1 2) '(2))> (edit-distance '(2) '())< 1> (edit-distance '(1 2) '())< 2> (edit-distance '(2) '(2))> >(edit-distance '() '())< <0> >(edit-distance '(2) '())< <1> >(edit-distance '() '(2))< <1< 0<11
7 calls to edit-distance
> (edit-distance (list 1 2) (list 2 3))>(edit-distance '(1 2) '(2 3))> (edit-distance '(2) '(3))> >(edit-distance '() '())< <0> >(edit-distance '(2) '())< <1> >(edit-distance '() '(3))< <1< 1> (edit-distance '(1 2) '(3))> >(edit-distance '(2) '())< <1> >(edit-distance '(1 2) '())< <2> >(edit-distance '(2) '(3))> > (edit-distance '() '())< < 0> > (edit-distance '(2) '())< < 1> > (edit-distance '() '(3))< < 1< <1< 2> (edit-distance '(2) '(2 3))> >(edit-distance '() '(3))< <1> >(edit-distance '(2) '(3))> > (edit-distance '() '())< < 0> > (edit-distance '(2) '())< < 1> > (edit-distance '() '(3))< < 1< <1> >(edit-distance '() '(2 3))< <2< 1<2
2
19 calls to edit-distance
(define (edit-distance s1 s2) (if (or (null? s1) (null? s2)) (+ (length s1) (length s2)) (min (+ (if (eq? (car s1) (car s2) 0 1) ; match or mutate (edit-distance (cdr s1) (cdr s2)) (+ 1 (edit-distance s1 (cdr s2))) ; insert in s1 (+ 1 (edit-distance (cdr s1) s2))))) ; delete from s1
(define (edit-distance-counter s1 s2) (if (or (null? s1) (null? s2)) 1 (+ 1 (edit-distance-counter (cdr s1) (cdr s2)) (edit-distance-counter s1 (cdr s2)) (edit-distance-counter (cdr s1) s2))))
(define (edit-distance s1 s2) (if (or (null? s1) (null? s2)) (+ (length s1) (length s2)) (min (+ (if (eq? (car s1) (car s2) 0 1) ; match or mutate (edit-distance (cdr s1) (cdr s2)) (+ 1 (edit-distance s1 (cdr s2))) ; insert in s1 (+ 1 (edit-distance (cdr s1) s2))))) ; delete from s1
Evaluation: Input Lists Size (each list) Calls to edit-distance
0 1
1 4
2 19
3 94
4 481
5 2,524
6 13,483
7 72,958
8 398,593
9 2,193,844
10 12,146,179
1 2 3 4 5 6 7 8 9 10 11 12 130
500,000,000
1,000,000,000
1,500,000,000
2,000,000,000
2,500,000,000
Input Size
Num
ber o
f ed
it-di
stan
ce A
pplic
ation
s
12,146,179
0
50,000,000
100,000,000
150,000,000
200,000,000
250,000,000
300,000,000Computing Power 1969-2011(in Apollo Control Computer Units)
a c a t c a t g c
0 1 2 3 4 5 6 7 8 9
c 1
a 2
t 3
g 4
a 5
t 6
g 7
s1
s2
a c a t c a t g c
0 1 2 3 4 5 6 7 8 9
c 1
a 2
t 3
g 4
a 5
t 6
g 7
s1
s2
Value in square [i, j] = min (1 + value in square [i-1, j], 1 + value in square [i, j-1], (0 or 1) + value in square [i-1, j-1])
a c a t c a t g c
0 1 2 3 4 5 6 7 8 9
c 1 1 1 2 3 4 5 6 7 8
a 2 2 2 1 2 3 4 5 6 7
t 3 3 3 2 1 3 4 5 6 7
g 4 4 4 4 2 2 3 4 5 6
a 5 5 5 5 4 3 2 3 4 5
t 6 6 6 6 5 4 3 2 3 4
g 7 7 7 7 6 5 4 3 2 3
s1
s2
Is this only used for genome analysis?
ChargeProblem Set 3: Posted now, due next Friday
Remember to either (1) have a partner you agree to work with in place, or (2) email me a partner/solo request by tomorrow
top related