SlideShare une entreprise Scribd logo
1  sur  11
Lists of Lists of Lists of ... CS 5010 Program Design Paradigms “Bootcamp” Week 03, Lesson 4 TexPoint fonts used in EMF.  Read the TexPoint manual before you delete this box.: AAA 1
Lists of Lists of ... strings "alice" "bob" "carole" ("alice" "bob") (("alice" "bob") "carole") ("alice"  (("alice" "bob") "dave") "eve")
Data Definition An S-expression of Strings (SoS) is either -- a String -- a List of SoS's A List of SoS's (LoSS) is either -- empty -- (cons SoSLoSS)
This is mutual recursion SoSLoSS defined in terms of  defined in terms of
Mutually Recursive Data Definitions One task, one function So: one function per data definition Here, functions come in pairs Write the contracts and purpose statements together, or Write one, and the other one will appear as a wishlist function
Template: functions come in pairs ;; sos-fn: SoS-> ?? (define (sos-fns)  (cond    [(string? s) ...]    [else (loss-fn s)])) ;; loss-fn: LoSS-> ?? (define (loss-fn los)   (cond     [(empty? los) ...]     [else (... (sos-fn(first los))                (loss-fn(rest los)))]))
occurs-in? ;; occurs-in? : Sos String -> Boolean ;; returns true if the given string occurs somewhere in the given sos. ;; occurs-in-loss? : Loss String -> Boolean ;; returns true if the given string occurs somewhere in the given loss.
Examples/Tests (check-expect (occurs-in? "alice" "alice") true) (check-expect (occurs-in? "bob"  "alice") false) (check-expect   (occurs-in? (list "alice" "bob") "cathy")   false) (check-expect   (occurs-in?    (list    (list "alice" "bob")     "carole")    "bob")   true) (check-expect   (occurs-in?    (list "alice"     (list (list "alice" "bob") "dave")     "eve")   "bob")  true) See book re list vs. cons
The Code (define (occurs-in? sosstr)   (cond     [(string? sos) (string=? sosstr)]     [else (occurs-in-loss? sosstr)])) (define (occurs-in-loss? loss str)   (cond     [(empty? loss) false]     [else (or (occurs-in? (first loss) str)               (occurs-in-loss? (rest loss) str))]))
Finger Exercises ;; number-of-strings : Sos -> Number ;; number-of-strings-in-loss : Loss -> Number ;; returns the number of strings in the given sos or loss. ;; characters-in : Sos -> Number ;; characters-in-loss : Loss -> Number ;; returns the total number of characters in the strings in the given sos or loss.
Summary Trees  of structures occur all the time Mutually recursive data definitions Mutual recursion in the data definition leads to mutual recursion in the template Mutual recursion in the template leads to mutual recursion in the code Study the tests:   always ask:  how could a program pass the tests and still be wrong? always ask: how could a program fail the tests and still be correct?

Contenu connexe

Similaire à Lesson 04 Lists Of Lists Of Lists

Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Contextlichtkind
 
Using Regular Expressions and Staying Sane
Using Regular Expressions and Staying SaneUsing Regular Expressions and Staying Sane
Using Regular Expressions and Staying SaneCarl Brown
 
Re Inventing Query Language
Re Inventing Query LanguageRe Inventing Query Language
Re Inventing Query LanguageRuslan Zakirov
 
A Scala Corrections Library
A Scala Corrections LibraryA Scala Corrections Library
A Scala Corrections LibraryPaul Phillips
 

Similaire à Lesson 04 Lists Of Lists Of Lists (7)

Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
 
TDDBC お題
TDDBC お題TDDBC お題
TDDBC お題
 
Using Regular Expressions and Staying Sane
Using Regular Expressions and Staying SaneUsing Regular Expressions and Staying Sane
Using Regular Expressions and Staying Sane
 
Re Inventing Query Language
Re Inventing Query LanguageRe Inventing Query Language
Re Inventing Query Language
 
4.1 PHP Arrays
4.1 PHP Arrays4.1 PHP Arrays
4.1 PHP Arrays
 
A Scala Corrections Library
A Scala Corrections LibraryA Scala Corrections Library
A Scala Corrections Library
 
Lecture2
Lecture2Lecture2
Lecture2
 

Lesson 04 Lists Of Lists Of Lists

  • 1. Lists of Lists of Lists of ... CS 5010 Program Design Paradigms “Bootcamp” Week 03, Lesson 4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1
  • 2. Lists of Lists of ... strings "alice" "bob" "carole" ("alice" "bob") (("alice" "bob") "carole") ("alice" (("alice" "bob") "dave") "eve")
  • 3. Data Definition An S-expression of Strings (SoS) is either -- a String -- a List of SoS's A List of SoS's (LoSS) is either -- empty -- (cons SoSLoSS)
  • 4. This is mutual recursion SoSLoSS defined in terms of defined in terms of
  • 5. Mutually Recursive Data Definitions One task, one function So: one function per data definition Here, functions come in pairs Write the contracts and purpose statements together, or Write one, and the other one will appear as a wishlist function
  • 6. Template: functions come in pairs ;; sos-fn: SoS-> ?? (define (sos-fns) (cond [(string? s) ...] [else (loss-fn s)])) ;; loss-fn: LoSS-> ?? (define (loss-fn los) (cond [(empty? los) ...] [else (... (sos-fn(first los)) (loss-fn(rest los)))]))
  • 7. occurs-in? ;; occurs-in? : Sos String -> Boolean ;; returns true if the given string occurs somewhere in the given sos. ;; occurs-in-loss? : Loss String -> Boolean ;; returns true if the given string occurs somewhere in the given loss.
  • 8. Examples/Tests (check-expect (occurs-in? "alice" "alice") true) (check-expect (occurs-in? "bob" "alice") false) (check-expect (occurs-in? (list "alice" "bob") "cathy") false) (check-expect (occurs-in? (list (list "alice" "bob") "carole") "bob") true) (check-expect (occurs-in? (list "alice" (list (list "alice" "bob") "dave") "eve") "bob") true) See book re list vs. cons
  • 9. The Code (define (occurs-in? sosstr) (cond [(string? sos) (string=? sosstr)] [else (occurs-in-loss? sosstr)])) (define (occurs-in-loss? loss str) (cond [(empty? loss) false] [else (or (occurs-in? (first loss) str) (occurs-in-loss? (rest loss) str))]))
  • 10. Finger Exercises ;; number-of-strings : Sos -> Number ;; number-of-strings-in-loss : Loss -> Number ;; returns the number of strings in the given sos or loss. ;; characters-in : Sos -> Number ;; characters-in-loss : Loss -> Number ;; returns the total number of characters in the strings in the given sos or loss.
  • 11. Summary Trees of structures occur all the time Mutually recursive data definitions Mutual recursion in the data definition leads to mutual recursion in the template Mutual recursion in the template leads to mutual recursion in the code Study the tests: always ask: how could a program pass the tests and still be wrong? always ask: how could a program fail the tests and still be correct?

Notes de l'éditeur

  1. Welcome to Week 3, Lesson 4: Arbitrarily Nested ListsSo far we've dealt with structures, structures with lists in them, and lists of structures.Now let's look at lists of lists.