CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3

Slides:



Advertisements
Similar presentations
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Advertisements

Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Working with images and scenes CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
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 ©
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.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
Lists of Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
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.
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.
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.
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
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.2
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
Examining Two Pieces of Data
More About Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.4
CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.3
Introduction to Invariants
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.1
From Templates to Folds
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.3
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
More examples of invariants
ormap, andmap, and filter
Generalizing Similar Functions
The Iterative Design Recipe
CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1
Rewriting your function using map and foldr
Working with images and scenes
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
When do I need an invariant?
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
Presentation transcript:

CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 Lists of Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 © Mitchell Wand, 2012-2017 This work is licensed under a 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 Write the data definition and template for S-expressions Write functions on S-expressions using the template

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. Another way of thinking of it is as a multi-way tree, except that the data is all at the leaves instead of in the interior nodes.

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 (1927-2011) for the programming language Lisp in 1958. McCarthy invented Lisp to solve problems in artificial intelligence. 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. S-expressions are one of the great inventions of modern programming. They were the original idea from which things like XML and JSON grew.

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

Examples 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. "alice" "bob" "carole" ("alice" "bob") (("alice" "bob") "carole") ("dave" ("alice" "bob") "carole") (("alice" "bob") (("ted" "carole")))

Data Definition An Sexp is either -- a String (any string will do), or -- an SexpList An SexpList is either -- empty -- (cons Sexp SexpList) Here we’ve built S-expressions where the basic data is strings, but we could build S-expressions of numbers, cats, sardines, or whatever. We’ll see that later in this lesson.

This is mutual recursion Sexp SexpList may contain

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. "alice" "bob"

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".

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

Observer Template: functions come in pairs ;; sexp-fn : Sexp -> ?? ;; slist-fn : SexpList -> ?? (define (sexp-fn s) (cond [(string? s) ...] [else (... (slist-fn s))])) (define (slist-fn sexps) [(empty? sexps) ...] [else (... (sexp-fn (first sexps)) (slist-fn (rest sexps)))]))

Remember: the shape of the program follows the shape of the data may contain SexpList Sexp Shape of the Data

Remember: the shape of the program follows the shape of the data may call slist-fn sexp-fn Shape of the Program

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

occurs-in? ;; occurs-in? : Sexp String -> Boolean ;; returns true iff the given string occurs somewhere in the given Sexp. ;; occurs-in-slist? : SexpList String -> Boolean ;; returns true iff the given string occurs somewhere in the given list of Sexps. Here's an example of a pair of related functions: occurs-in? , which works on a Sexp, and occurs-in-slist? , which works on a SexpList.

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

More Examples ;; number-of-strings-in-sexp : Sexp -> Number ;; number-of-strings-in-sexps : SexpList -> Number ;; returns the number of strings in the given Sexp or SexpList. ;; characters-in-sexp : Sexp -> Number ;; characters-in-sexps : SexpList -> Number ;; returns the total number of characters in the strings in the given Sexp or SexpList.

The S-expression pattern Can do this for things other than strings: An XSexp is either -- an X -- an XSexpList A XSexpList is either -- empty -- (cons XSexp XSexpList)

The Template for XSexp ;; sexp-fn : XSexp-> ?? (define (sexp-fn s) (cond [(X? s) ...] [else (slist-fn s)])) ;; slist-fn : XSexpList -> ?? (define (slist-fn sexps) [(empty? sexps) ...] [else (... (sexp-fn (first sexps)) (slist-fn (rest sexps)))])) (first sexps) is a XSexp. This is mixed data, so our rule about the shape of the program following the shape of the data tells us that we should expect to wrap it in an (sexp-fn ...) .

Sexps with Sardines as the data A SardineSexp is either -- a Sardine -- a SardineSexpList A SardineSexpList is either -- empty -- (cons SardineSexp SardineSexpList) An example of the XSexp pattern.

The Template for SardineSexp ;; sardine-sexp-fn : SardineSexp -> ?? (define (sardine-sexp-fn s) (cond [(sardine? s) ...] [else (sardine-sexp-list-fn s)])) ;; sardine-sexp-list-fn : SardineSexpList -> ?? (define (sardine-sexp-list-fn sexps) [(empty? sexps) ...] [else (... (sardine-sexp-fn (first sexps)) (sardine-sexp-list-fn (rest sexps)))]))

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

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

Next Steps Study the file 05-3-sexps.rkt in the Examples folder If you have questions about this lesson, ask them on the Discussion Board Do Guided Practice 5.3 Go on to the next lesson