Lists of Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5 1 © Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons Attribution-NonCommercial.

Slides:



Advertisements
Similar presentations
When do I need an invariant? CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Advertisements

Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Ormap, andmap, and filter CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Structural Decomposition CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative Commons.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
From Templates to Folds CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Examining Two Pieces of Data CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
The Point of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.1 © Mitchell Wand, This work is licensed under a Creative Commons.
Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Design Strategies 2: Using a template CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Generalizing Over Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Lists of Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Destructor Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson
Testing Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
The DD  OO Recipe CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.4 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you.
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
Classes, Objects, and Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
How to use an Observer Template
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
More About Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.3
More Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.7
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2
Halting Functions for Tree-Like Structures
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1
From Templates to Folds
Case Study: Undefined Variables
CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5
Solving Your Problem by Generalization
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
Let's Play "What's the Question"
More examples of invariants
ormap, andmap, and filter
Generalizing Similar Functions
Rewriting your function using map and foldr
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3
Presentation transcript:

Lists of Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Creative Commons Attribution-NonCommercial 4.0 International License

Learning Outcomes At the end of this lesson, the student should be able to – Give examples of S-expressions – Give 3 reasons why S-expressions are important – Write the data definition and template for S- expressions – Write functions on S-expressions using the template 2

S-expressions (informally) An S-expression is something that is either a string or a list of S-expressions. So if it's a list, it could contain strings, or lists of strings, or lists of lists of strings, etc. Think of it as a nested list, where there's no bound on how deep the nesting can get. 3

Some History An S-expression is a kind of nested list, that is, a list whose elements may be other lists. Here is an informal history of S-expressions. S-expressions were invented by John McCarthy ( ) for the programming language Lisp in McCarthy invented Lisp to solve problems in artificial intelligence.John McCarthy Lisp introduced lists, S-expressions, and parenthesized syntax. The syntax of Lisp and its descendants, like Racket, is based on S-expressions. The use of S-expressions for syntax makes it easy to read and write programs: all you have to do is balance parentheses. This is much simpler than the syntax of other programming languages, which have semicolons and other rules that can make programs harder to read.harder to read S-expressions are one of the great inventions of modern programming. They were the original idea from which things like XML and JSON grew. 4

Examples "alice" "bob" "carole" (list "alice" "bob") (list (list "alice" "bob") "carole") (list "dave" (list "alice" "bob") "carole") (list (list "alice" "bob") (list (list "ted" "carole"))) Here are some examples of S- expressions, in list notation (See Lesson 4.1)Lesson 4.1 5

Examples "alice" "bob" "carole" ("alice" "bob") (("alice" "bob") "carole") ("dave" ("alice" "bob") "carole") (("alice" "bob") (("ted" "carole"))) Here are the same examples of S-expressions, in write notation (See Lesson 4.1). We often use write notation because it is more compact.Lesson 4.1 6

Data Definition An S-expression of Strings (SoS) is either -- a String -- a List of SoS's (LoSS) A List of SoS's (LoSS) is either -- empty -- (cons SoS LoSS) Let's write down a precise definition: An S-expression is either a string or a list of S-expressions A list of S-expressions is either empty or the cons of an S- expressions and another list of S- expressions. Note that the data definition for "list of S-expressions" follows the familiar pattern for lists. These two definitions are mutually recursive, as you can see from the two arrows 7

This is mutual recursion SoS LoSS defined in terms of 8

Data Structures "alice" "bob" "carole" ("alice" "bob") "alice""bob" A list of S-expressions is implemented as a singly-linked list. Here is an example. 9

Data Structures (("alice" "bob") "carole") "carole" "alice""bob" Here is a slightly more complicated example. Observe that the first of this list is another list. The first of the first is the string "alice". 10

Data Structures (cont'd) ("alice" (("alice" "bob") "carole") "dave") "carole" "alice""bob" "alice""dave" Here is a still more complicated example. 11

The template recipe QuestionAnswer Does the data definition distinguish among different subclasses of data? Your template needs as many cond clauses as subclasses that the data definition distinguishes.cond How do the subclasses differ from each other? Use the differences to formulate a condition per clause. Do any of the clauses deal with structured values? If so, add appropriate selector expressions to the clause. Does the data definition use self- references? Formulate ``natural recursions'' for the template to represent the self-references of the data definition. Do any of the fields contain compound or mixed data? If the value of a field is a foo, add a call to a foo-fn to use it. Remember the template recipe 12

Template: functions come in pairs ;; sos-fn : SoS -> ?? (define (sos-fn s) (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)))])) (first los) is a SoS. This is mixed data, so the last rule in the template recipe tells us we need to wrap it in a (sos-fn...). 13

This is mutual recursion sos-fn loss-fn defined in terms of 14

One function, one task Each function deals with exactly one data definition. So functions will come in pairs Write contracts and purpose statements together, or Write one, and the other one will appear as a wishlist function 15

occurs-in? ;; occurs-in? : SoS String -> Boolean ;; returns true iff the given string occurs somewhere in the given sos. ;; occurs-in-loss? : LoSS String -> Boolean ;; returns true iff the given string occurs somewhere in the given loss. Here's an example of a pair of related functions: occurs-in?, which works on a SoS, and occurs-in-loss?, which works on a LoSS. 16

Examples/Tests (check-equal? (occurs-in? "alice" "alice") true) (check-equal? (occurs-in? "bob" "alice") false) (check-equal? (occurs-in? (list "alice" "bob") "cathy") false) (check-equal? (occurs-in? (list (list "alice" "bob") "carole") "bob") true) (check-equal? (occurs-in? (list "alice" (list (list "alice" "bob") "dave") "eve") "bob") true) 17

sos-and-loss.rkt 18

sos-and-loss.rkt 19 The inspiration for this livecoding exercise comes from here or here. Background informationhere Background information

Livecoding: sos-and-loss.rkt occurs-in? : number-of-strings : jdukgRx4http://youtu.be/9z- jdukgRx4 The inspiration for this livecoding exercise comes from here and here (ignore the sappy music). Background informationhere Background information 20

The S-expression pattern Can do this for things other than strings: An Sexp is either -- an X -- a ListOf > A ListOf > is either -- empty -- (cons Sexp ListOf >) 21

The Template for Sexp ;; sexp-fn : Sexp -> ?? (define (sexp-fn s) (cond [(X? s)...] [else (losexp-fn s)])) ;; losexp-fn : ListOf > -> ?? (define (losexp-fn los) (cond [(empty? los)...] [else (... (sexp-fn (first los)) (losexp-fn (rest los)))])) (first los) is a Sexp. This is mixed data, so the last rule in the template recipe tells us we need to wrap it in a (sexp-fn...). 22

Sexp of Sardines An SoSardines is either -- a Sardine -- a LoSSardines A LoSSardines is either -- empty -- (cons SoSardines LoSSardines) An Example of the Sexp pattern. 23

The Template for SoSardines ;; sosard-fn : SoSardines -> ?? (define (sosard-fn s) (cond [(sardine? s)...] [else (lossard-fn s)])) ;; lossard-fn : LoSSardines -> ?? (define (lossard-fn los) (cond [(empty? los)...] [else (... (sosard-fn (first los)) (lossard-fn (rest los)))])) 24

Summary Nested Lists 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 25

More Examples ;; 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. ;; number-of-sardines : SoSardines -> Number ;; returns the total number of sardines in the given SoSardines. 26

Summary You should now be able to: – Give examples of S-expressions – Give 3 reasons why S-expressions are important – Write the data definition and template for S- expressions – Write functions on S-expressions using the template 27

Next Steps If you have questions about this lesson, ask them on the Discussion Board Do Guided Practice 6.5Guided Practice 6.5 Go on to the next lesson 28