Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 1 © Mitchell Wand, 2012-2015 This work is licensed under a Creative Commons.

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

Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Basics of Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 © Mitchell Wand, This work is licensed under a Creative Commons.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
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.:
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © Mitchell Wand, This work is licensed under a Creative.
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.
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.
Patterns of Communication Between Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.1 © Mitchell Wand, This work is licensed under.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © 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.
Design Strategies 3: Divide into cases CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
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.
The 8-queens problem 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.
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.
Organization of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.2 © Mitchell Wand, This work is licensed under a Creative Commons.
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.:
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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
More Recursive Data Types
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
The Different Kinds of Data
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
Rewriting your function using map and foldr
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
Design Strategies 3: Divide into cases
Presentation transcript:

Lists vs. Structures 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

Module Introduction In this module we will learn about two related topics: – branching structures, such as trees – mutually recursive data definitions, such as lists of alternating strings and numbers. 2

Module Outline Lesson 6.1 begins by considering alternative representations for sequence information – This is a warm-up for Lessons Lessons 6.2 and 6.3 show how to represent information that has a naturally branching structure, such as trees Lesson 6.4 introduces mutually-recursive data definitions Lesson 6.5 applies these ideas to S-expressions – S-expressions are nested lists – These are the basis for XML and JSON Lesson 6.6 combines all these ideas into a case study 3

Generalization Over Constants Over Expressions Over Contexts Over Data Representations Over Method Implementations Mixed Data Data Representations Basics Recursive Data Functional Data Objects & Classes Stateful Objects Design Strategies Combine simpler functions Use a template Divide into Cases Call a more general function Communicate via State Module 06 4

Lesson Introduction We've already studied how to represent sequences of data using lists. In this lesson, we will explore how to represent sequences of data using structures, like those we studied in Week 1, instead of lists. This is useful because many widely-used languages do not have built-in lists that we can use. 5

Learning Objectives for this Lesson At the end of this lesson the student should be able to: – convert a data definition using the ListOfX pattern to a recursive data definition using structures – write a template for a recursive data definition using structures 6

Recall our pizzas ;; A Topping is a String. ;; A Pizza is a ListOfTopping ;; interp: a pizza is a list of toppings, listed from top to bottom ;; pizza-fn : Pizza -> ?? ; Given a Pizza, produce.... ;; (define (pizza-fn p) ;; (cond ;; [(empty? p)...] ;; [else (... (first p) ;; (pizza-fn (rest p)))])) ;; Examples: (define plain-pizza empty) (define cheese-pizza (list "cheese")) (define anchovices-cheese-pizza (list "anchovies" "cheese") 7 In Module 4, we represented a pizza as a list of toppings. This week, we will use this example to study the structure representation.

What if Racket didn't have cons? If Racket didn't have cons, we could still represent pizzas as mixed data, using a structure to represent a non-empty pizza. On the next slide, we'll see what the data definition would look like. We haven't written the template yet; we'll get to that soon. 8

What if Racket didn't have cons? We could still write a data definition: (define-struct plain-pizza ()) (define-struct topped-pizza (topping base)) A Topping is a String. A Pizza is either -- (make-plain-pizza) -- (make-topped-pizza Topping Pizza) Interp: (make-plain-pizza) represents a pizza with no toppings (make-topped-pizza t p) represents a pizza like p, but with topping t added on top. 9 This representation, using a set of alternatives each of which is a struct, is a standard strategy, sometimes called the "sum of products" representation. HINT: You won't go wrong if you use this as your default representation for data in Racket.

This data definition is self-referential (define-struct topped-pizza (topping base)) A Topping is a String. A Pizza is either -- (make-plain-pizza) -- (make-topped-pizza Topping Pizza) 10 compare: A ListOfToppings is either -- empty -- (cons Topping ListOfToppings) compare: A ListOfToppings is either -- empty -- (cons Topping ListOfToppings) This data definition is self-referential, just like ListofToppings was.

Examples (make-plain-pizza) (make-topped-pizza "cheese" (make-plain-pizza)) (make-topped-pizza "anchovies" (make-topped-pizza "cheese" (make-plain-pizza)))) (make-topped-pizza "onions" (make-topped-pizza "anchovies" (make-topped-pizza "cheese" (make-plain-pizza)))))) 11 Here are some examples of pizzas according to our new data definition. Can you see why each of these is a Pizza, according to our new definition? A Pizza is either -- (make-plain-pizza) -- (make-topped-pizza Topping Pizza)

Template for pizza functions pizza-fn : Pizza -> ?? (define (pizza-fn p) (cond [(plain-pizza? p)...] [else (... (topped-pizza-topping p) (pizza-fn (topped-pizza-base p)))])) 12

This template is self-referential pizza-fn : Pizza -> ?? (define (pizza-fn pizza) (cond [(plain-pizza? pizza)...] [else (... (topped-pizza-topping pizza) (pizza-fn (topped-pizza-base pizza)))])) 13 We also call this a recursive template

Lists vs Structures: Data Definitions A ListOfToppings (LoT) is either -- empty -- (cons Topping LoT) Interp: -- empty means a pizza with no toppings -- (cons t p) represents the pizza p with topping t added on top. A Pizza is either -- (make-plain-pizza) -- (make-topped-pizza Topping Pizza) Interp: (make-plain-pizza) means a pizza with no toppings (make-topped-pizza t p) represents the pizza p with topping t added on top. 14 Observe that both data definitions are self- referential in the same way. You could represent pizzas either by lists or structures.

Lists vs. Structures: Templates pizza-fn : Pizza -> ?? (define (pizza-fn p) (cond [(empty? p)...] [else (... (first p) (pizza-fn (rest p)))])) pizza-fn : Pizza -> ?? (define (pizza-fn p) (cond [(plain-pizza? p)...] [else (... (topped-pizza- topping p) (pizza-fn (topped-pizza-base p)))])) 15 And here are the templates. Observe that they are also both self-referential in the same way.

Lists vs. Structures: The Choice Use structures for compound information with a fixed size or fixed number of components. Use lists for homogeneous sequences of data items. – so we'll use mostly lists – DON’T use lists for data of fixed size or a fixed number of components Each language has its own idioms – some don't have lists at all – some have other ways of representing sequences– use them when possible 16

Summary You should now be able to – convert a data definition using the ListOfX pattern to a recursive data definition using structures – write a template for a recursive data definition using structures 17

Next Steps Study the file 06-1-recursive-structures.rkt in the Examples folder. If you have questions about this lesson, ask them on the Discussion Board Do Guided Practice 6.1 Go on to the next lesson 18