SlideShare a Scribd company logo
Class 10:
Abstracting
Procedures




              cs1120 Fall 2011
              David Evans
              14 September 2011
Plan for Today
PS2 Due Now
Abstracting List Procedures
PS2: 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 to
      email 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> ]”
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




                                                       3
list-sum


(define (list-sum p)
  (if (null? p)
     0
     (+ (car p) (list-sum (cdr p)))))


             Okay, what about list-product?


                                              4
list-product


(define (list-product p)
  (if (null? p)
     1
     (* (car p) (list-product (cdr p)))))


             Okay, what about list-length?


                                             5
list-length


(define (list-length p)
  (if (null? p)
     0
     (+ 1 (list-length (cdr p)))))




                                     6
Comparing List Procedures
(define (is-list? p)                (define (list-sum p)
  (if (null? p)                       (if (null? p)
     true                                0
     (if (pair? p)                       (+ (car p)
         (is-list? (cdr p))                 (list-sum (cdr p)))))
         false)))

(define (list-product p)            (define (list-length p)
  (if (null? p)                       (if (null? p)
     1                                   0
     (* (car p)                          (+ 1 (list-length (cdr p)))))
        (list-product (cdr p)))))

                                                                         7
Base Cases

(define (is-list? p)                       (define (list-sum p)
  (if (null? p)                               (if (null? p)
     true                                        0
     (if (pair? p) (is-list? (cdr p)) false))) (+ (car p) (list-sum (cdr p)))))


(define (list-product p)                  (define (list-length p)
  (if (null? p)                             (if (null? p)
     1                                         0
     (* (car p) (list-product (cdr p)))))      (+ 1 (list-length (cdr p)))))


                                                                                  8
Recursive Calls

(define (is-list? p)                        (define (list-sum p)
  (if (null? p)                               (if (null? p)
     true                                        0
     (if (pair? p) (is-list? (cdr p)) false))) (+ (car p) (list-sum (cdr p)))))


  (define (list-product p)                 (define (list-length p)
    (if (null? p)                            (if (null? p)
       1                                        0
       (* (car p) (list-product (cdr p)))))     (+ 1 (list-length (cdr p)))))
                      What does each do with the car of the list?

                                                                                  9
Abstracted List Procedure
(define (list-sum p)                    (define (list-product p)
  (if (null? p)                           (if (null? p)
     0                                       1
     (+ (car p) (list-sum (cdr p)))))        (* (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-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
                                        Million
                                        Bases




        http://mbgd.genome.ad.jp/CoreAligner/
Defining edit-distance
(define (edit-distance s1 s2)
 (if (and (null? s1) (null? s2))
     0
     (if (null? s1)                Base cases
        (length s2)
        (if (null? s2)
            (length s1)
            …
(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
(edit-distance                                                                                                                                              )
                                                    c           a           t           g           a       t           a           c


    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)                > (edit-distance (list 1 2) (list 2 3))
> (trace edit-distance)                 >(edit-distance '(1 2) '(2 3))
                                        > (edit-distance '(2) '(3))

> (edit-distance (list 1 2) (list 2))   > >(edit-distance '() '())
                                        < <0
                                        > >(edit-distance '(2) '())
>(edit-distance '(1 2) '(2))            < <1
                                        > >(edit-distance '() '(3))
                                        < <1
> (edit-distance '(2) '())              <1
                                        > (edit-distance '(1 2) '(3))
<1                                      > >(edit-distance '(2) '())
                                        < <1
                                        > >(edit-distance '(1 2) '())
> (edit-distance '(1 2) '())            < <2
                                        > >(edit-distance '(2) '(3))

<2                                      > > (edit-distance '() '())
                                        <<0
                                        > > (edit-distance '(2) '())
> (edit-distance '(2) '(2))             <<1
                                        > > (edit-distance '() '(3))

> >(edit-distance '() '())              <<1
                                        < <1
                                        <2
< <0                                    > (edit-distance '(2) '(2 3))
                                        > >(edit-distance '() '(3))
                                        < <1
> >(edit-distance '(2) '())             > >(edit-distance '(2) '(3))
                                        > > (edit-distance '() '())
< <1                                    <<0
                                        > > (edit-distance '(2) '())
                                        <<1
> >(edit-distance '() '(2))             > > (edit-distance '() '(3))
                                        <<1

< <1                                    < <1
                                        > >(edit-distance '() '(2 3))
                                        < <2
<0                                      <1
                                        <2

<1                                      2
1

  7 calls to edit-distance                     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 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))))
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
Computing Power 1969-2011
                                                 300,000,000
                                                               (in Apollo Control Computer Units)
                             2,500,000,000
edit-distance Applications                       250,000,000
                                                 200,000,000
                             2,000,000,000       150,000,000
                                                 100,000,000
        Number of



                             1,500,000,000        50,000,000
                                                          0

                             1,000,000,000



                              500,000,000
                                                                                    12,146,179

                                        0
                                             1   2    3        4    5     6     7     8     9       10   11   12   13


                                                                              Input Size
s1


             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

s2   g   4

     a   5

     t   6

     g   7
s1


             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           Value in square [i, j] =
s2   g   4            min (1 + value in square [i-1, j],
                            1 + value in square [i, j-1],
     a   5                  (0 or 1) + value in square [i-1, j-1])
     t   6

     g   7
s1


             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

s2   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
Is this only used for genome analysis?
Charge
Problem 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

More Related Content

PPTX
Class 11: Deeper List Procedures
TXT
Procesos
DOCX
PDF
Crabs dataset GLM Poisson and Logistic model
DOC
Os 2 cycle
PDF
Cn os-lp lab manual k.roshan
PDF
The Ring programming language version 1.9 book - Part 114 of 210
PDF
5th Semester Mechanical Engineering (2013-June) Question Papers
Class 11: Deeper List Procedures
Procesos
Crabs dataset GLM Poisson and Logistic model
Os 2 cycle
Cn os-lp lab manual k.roshan
The Ring programming language version 1.9 book - Part 114 of 210
5th Semester Mechanical Engineering (2013-June) Question Papers

What's hot (13)

PPSX
Scala @ TomTom
PDF
Query Rewriting and Optimization for Ontological Databases
PDF
8th Semester (June; July-2014) Mechanical Engineering Question Papers
PDF
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
PDF
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
PDF
8th Semester (Dec-2015; Jan-2016) Computer Science and Information Science En...
PDF
5th Semester (Dec-2015; Jan-2016) Computer Science and Information Science En...
PDF
The Ring programming language version 1.5.1 book - Part 73 of 180
PDF
The Ring programming language version 1.9 book - Part 94 of 210
PDF
Memory efficient pytorch
PDF
Volume and edge skeleton computation in high dimensions
PDF
7th Semester Mechanical Engineering (June/July-2015) Question Papers
PDF
3rd Semester Mechanical Engineering (June-2016) Question Papers
Scala @ TomTom
Query Rewriting and Optimization for Ontological Databases
8th Semester (June; July-2014) Mechanical Engineering Question Papers
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
8th Semester (Dec-2015; Jan-2016) Computer Science and Information Science En...
5th Semester (Dec-2015; Jan-2016) Computer Science and Information Science En...
The Ring programming language version 1.5.1 book - Part 73 of 180
The Ring programming language version 1.9 book - Part 94 of 210
Memory efficient pytorch
Volume and edge skeleton computation in high dimensions
7th Semester Mechanical Engineering (June/July-2015) Question Papers
3rd Semester Mechanical Engineering (June-2016) Question Papers
Ad

Similar to Class 10: Abstracting Procedures (20)

DOCX
Lisp and prolog in artificial intelligence
PDF
CL metaprogramming
PPTX
Class 24: Imperative Programming
PDF
The Magnificent Seven
DOCX
CDMA simulation code for wireless Network.docx
PDF
Abstract Meaning Representation
PDF
Microsoft Word Practice Exercise Set 2
PDF
SICP勉強会について
ODP
Recursion and Functional Programming
ODP
PDF
[系列活動] Data exploration with modern R
PDF
Hadoop I/O Analysis
PDF
Compiler Construction | Lecture 9 | Constraint Resolution
PDF
MongoDB Analytics
PDF
Introduction To Lisp
PPT
Algorithm.ppt
PDF
Machine Design MD By S K Mondal T&Q.0003.pdf
PDF
Anlysis and design of algorithms part 1
PPT
Asymptotic notations
PDF
Python grass
Lisp and prolog in artificial intelligence
CL metaprogramming
Class 24: Imperative Programming
The Magnificent Seven
CDMA simulation code for wireless Network.docx
Abstract Meaning Representation
Microsoft Word Practice Exercise Set 2
SICP勉強会について
Recursion and Functional Programming
[系列活動] Data exploration with modern R
Hadoop I/O Analysis
Compiler Construction | Lecture 9 | Constraint Resolution
MongoDB Analytics
Introduction To Lisp
Algorithm.ppt
Machine Design MD By S K Mondal T&Q.0003.pdf
Anlysis and design of algorithms part 1
Asymptotic notations
Python grass
Ad

More from David Evans (20)

PPTX
Cryptocurrency Jeopardy!
PPTX
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
PPTX
Hidden Services, Zero Knowledge
PPTX
Anonymity in Bitcoin
PPTX
Midterm Confirmations
PPTX
Scripting Transactions
PPTX
How to Live in Paradise
PPTX
Bitcoin Script
PPTX
Mining Economics
PPTX
Mining
PPTX
The Blockchain
PPTX
Becoming More Paranoid
PPTX
Asymmetric Key Signatures
PPTX
Introduction to Cryptography
PPTX
Class 1: What is Money?
PPTX
Multi-Party Computation for the Masses
PPTX
Proof of Reserve
PPTX
Silk Road
PPTX
Blooming Sidechains!
PPTX
Useful Proofs of Work, Permacoin
Cryptocurrency Jeopardy!
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Hidden Services, Zero Knowledge
Anonymity in Bitcoin
Midterm Confirmations
Scripting Transactions
How to Live in Paradise
Bitcoin Script
Mining Economics
Mining
The Blockchain
Becoming More Paranoid
Asymmetric Key Signatures
Introduction to Cryptography
Class 1: What is Money?
Multi-Party Computation for the Masses
Proof of Reserve
Silk Road
Blooming Sidechains!
Useful Proofs of Work, Permacoin

Class 10: Abstracting Procedures

  • 1. Class 10: Abstracting Procedures cs1120 Fall 2011 David Evans 14 September 2011
  • 2. Plan for Today PS2 Due Now Abstracting List Procedures PS2: 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 to email 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 3
  • 4. list-sum (define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p))))) Okay, what about list-product? 4
  • 5. list-product (define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p))))) Okay, what about list-length? 5
  • 6. list-length (define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p))))) 6
  • 7. Comparing List Procedures (define (is-list? p) (define (list-sum p) (if (null? p) (if (null? p) true 0 (if (pair? p) (+ (car p) (is-list? (cdr p)) (list-sum (cdr p))))) false))) (define (list-product p) (define (list-length p) (if (null? p) (if (null? p) 1 0 (* (car p) (+ 1 (list-length (cdr p))))) (list-product (cdr p))))) 7
  • 8. Base Cases (define (is-list? p) (define (list-sum p) (if (null? p) (if (null? p) true 0 (if (pair? p) (is-list? (cdr p)) false))) (+ (car p) (list-sum (cdr p))))) (define (list-product p) (define (list-length p) (if (null? p) (if (null? p) 1 0 (* (car p) (list-product (cdr p))))) (+ 1 (list-length (cdr p))))) 8
  • 9. Recursive Calls (define (is-list? p) (define (list-sum p) (if (null? p) (if (null? p) true 0 (if (pair? p) (is-list? (cdr p)) false))) (+ (car p) (list-sum (cdr p))))) (define (list-product p) (define (list-length p) (if (null? p) (if (null? p) 1 0 (* (car p) (list-product (cdr p))))) (+ 1 (list-length (cdr p))))) What does each do with the car of the list? 9
  • 10. Abstracted List Procedure (define (list-sum p) (define (list-product p) (if (null? p) (if (null? p) 0 1 (+ (car p) (list-sum (cdr p))))) (* (car p) (list-product (cdr p)))))
  • 11. 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)))))
  • 12. 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)))
  • 13. PS2: Edit Distance Million Bases http://mbgd.genome.ad.jp/CoreAligner/
  • 14. Defining edit-distance (define (edit-distance s1 s2) (if (and (null? s1) (null? s2)) 0 (if (null? s1) Base cases (length s2) (if (null? s2) (length s1) …
  • 15. (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
  • 16. (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.)
  • 17. Executing edit-distance a c a t c a t g c (edit-distance ) c a t g a t a c 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
  • 18. > (require racket/trace) > (edit-distance (list 1 2) (list 2 3)) > (trace edit-distance) >(edit-distance '(1 2) '(2 3)) > (edit-distance '(2) '(3)) > (edit-distance (list 1 2) (list 2)) > >(edit-distance '() '()) < <0 > >(edit-distance '(2) '()) >(edit-distance '(1 2) '(2)) < <1 > >(edit-distance '() '(3)) < <1 > (edit-distance '(2) '()) <1 > (edit-distance '(1 2) '(3)) <1 > >(edit-distance '(2) '()) < <1 > >(edit-distance '(1 2) '()) > (edit-distance '(1 2) '()) < <2 > >(edit-distance '(2) '(3)) <2 > > (edit-distance '() '()) <<0 > > (edit-distance '(2) '()) > (edit-distance '(2) '(2)) <<1 > > (edit-distance '() '(3)) > >(edit-distance '() '()) <<1 < <1 <2 < <0 > (edit-distance '(2) '(2 3)) > >(edit-distance '() '(3)) < <1 > >(edit-distance '(2) '()) > >(edit-distance '(2) '(3)) > > (edit-distance '() '()) < <1 <<0 > > (edit-distance '(2) '()) <<1 > >(edit-distance '() '(2)) > > (edit-distance '() '(3)) <<1 < <1 < <1 > >(edit-distance '() '(2 3)) < <2 <0 <1 <2 <1 2 1 7 calls to edit-distance 19 calls to edit-distance
  • 19. (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
  • 20. (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))))
  • 21. 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
  • 22. Computing Power 1969-2011 300,000,000 (in Apollo Control Computer Units) 2,500,000,000 edit-distance Applications 250,000,000 200,000,000 2,000,000,000 150,000,000 100,000,000 Number of 1,500,000,000 50,000,000 0 1,000,000,000 500,000,000 12,146,179 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Input Size
  • 23. s1 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 s2 g 4 a 5 t 6 g 7
  • 24. s1 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 Value in square [i, j] = s2 g 4 min (1 + value in square [i-1, j], 1 + value in square [i, j-1], a 5 (0 or 1) + value in square [i-1, j-1]) t 6 g 7
  • 25. s1 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 s2 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
  • 26. Is this only used for genome analysis?
  • 27. Charge Problem 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