SlideShare a Scribd company logo
1 of 18
Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Week 02, Lesson 2 TexPoint fonts used in EMF.  Read the TexPoint manual before you delete this box.: AAA 1
How to represent info of arbitrary size? phone book pixels in an image bombs dropped by a UFO slides in a presentation Approach: think about how such information is constructed.
Example: Pizza How do you construct a pizza?  Two possibilities: grab a pizza crust take a pizza and add a topping ...and so on
Data Definition (define-struct topped-pizza (topping base)) A Topping is a String. A Pizza is either -- the string "plain crust" -- (make-topped-pizza Topping Pizza)
Examples (define plain-pizza "plain crust") (define cheese-pizza   (make-topped-pizza "cheese" plain-pizza)) (define anchovies-cheese   (make-topped-pizza "anchovies" cheese-pizza)) (define onions-anchovies-cheese   (make-topped-pizza "onions" anchovies-cheese))
This data definition is self-referential (define-struct topped-pizza (topping base)) A Topping is a String. A Pizza is either -- the string "plain crust" -- (make-topped-pizza Topping Pizza) We also call this a recursive data definition
Template for pizza functions pizza-fn : Pizza -> ?? Given a Pizza, produce .... (define (pizza-fn pizza)   (cond     [(plain-pizza? pizza) ...]     [else (... (topped-pizza-topping pizza)                (pizza-fn  (topped-pizza-base pizza)))])) Add to wishlist: plain-pizza? : Pizza -> Boolean returns true iff the given pizza is a plain pizza
This template is self-referential pizza-fn : Pizza -> ?? Given a Pizza, produce .... (define (pizza-fn pizza)   (cond     [(plain-pizza? pizza) ...]     [else (... (topped-pizza-topping pizza)                (pizza-fn (topped-pizza-base pizza)))])) We also call this a recursive template
Example: number of toppings number-of-toppings : Pizza -> Number Given a Pizza, produce the number of toppings Examples: (number-of-toppings plain-pizza) = 0 (number-of-toppings cheese-pizza) = 1 (number-of-toppings anchovies-cheese) = 2 (number-of-toppings onions-anchovies-cheese) = 3
Example: number of toppings number-of-toppings : Pizza -> Number Given a Pizza, produce the number of toppings Strategy: structural decomposition (define (number-of-toppings pizza)   (cond     [(plain-pizza? pizza) ...]     [else (... (topped-pizza-topping pizza)                (number-of-toppings (topped-pizza-base pizza)))])) How many toppings does the plain pizza have? Answer: 0
Example: number of toppings number-of-toppings : Pizza -> Number Given a Pizza, produce the number of toppings Strategy: structural decomposition (define (number-of-toppings pizza)   (cond     [(plain-pizza? pizza) 0]     [else (... (topped-pizza-topping pizza)                (number-of-toppings (topped-pizza-base pizza)))])) How many toppings does the plain pizza have? Answer: 0
Example: number of toppings number-of-toppings : Pizza -> Number Given a Pizza, produce the number of toppings Strategy: structural decomposition (define (number-of-toppings pizza)   (cond     [(plain-pizza? pizza) 0]     [else (... (topped-pizza-topping pizza)                (number-of-toppings (topped-pizza-base pizza)))])) Q: If we knew the number of toppings on the pizza base, how many toppings would there on the whole pizza? Answer: one more topping!
Example: number of toppings number-of-toppings : Pizza -> Number Given a Pizza, produce the number of toppings (define (number-of-toppings pizza)   (cond     [(plain-pizza? pizza) 0]     [else (+ 1 (number-of-toppings (topped-pizza-base pizza)))])) Self-reference in the data definition leads to self-reference in the template; Self-reference in the template leads to self-reference in the code. Self-reference in the data definition leads to self-reference in the template; Self-reference in the template leads to self-reference in the code. Self-reference in the data definition leads to self-reference in the template; Self-reference in the template leads to self-reference in the code. Self-reference in the data definition leads to self-reference in the template; Self-reference in the template leads to self-reference in the code.
Don't forget the tests! ;; plain pizza (check-expect   (number-of-toppings plain-pizza)    0) ;; one topping (check-expect  (number-of-toppings cheese-pizza)   1) ...etc...
Steps for the design strategy What's the answer for the empty pizza? If you knew the answer for the base pizza, how would you get the answer for the whole pizza?
Many other examples add-anchovies : Pizza -> Pizza remove-all-anchovies : Pizza -> Pizza Produce a Pizza like the given one, but with all anchovies removed. replace-all-anchovies-with-onions : Pizza -> Pizza replace-all-anchovies : Pizza Topping -> Pizza replace-topping : Pizza Topping Topping -> Pizza Produce a Pizza like the given one, but with all instances of the first topping replaced by the second one.
Check the wishlist plain-pizza? : Pizza -> Boolean returns true iff the given pizza is a plain pizza ;; examples: ... ;; strategy: function composition (define (plain-pizza? pizza)   (and     (string? pizza)     (string=? pizza "plain crust"))) ;; tests: ....
Summary Represent arbitrary-sized information using a self-referential (or recursive) data definition. Self-reference in the data definition leads to self-reference in the template. Self-reference in the template leads to self-reference in the code. Follow the recipe!

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Week 02 Lesson 02 Recursive Data Definitions

  • 1. Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Week 02, Lesson 2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1
  • 2. How to represent info of arbitrary size? phone book pixels in an image bombs dropped by a UFO slides in a presentation Approach: think about how such information is constructed.
  • 3. Example: Pizza How do you construct a pizza? Two possibilities: grab a pizza crust take a pizza and add a topping ...and so on
  • 4. Data Definition (define-struct topped-pizza (topping base)) A Topping is a String. A Pizza is either -- the string "plain crust" -- (make-topped-pizza Topping Pizza)
  • 5. Examples (define plain-pizza "plain crust") (define cheese-pizza (make-topped-pizza "cheese" plain-pizza)) (define anchovies-cheese (make-topped-pizza "anchovies" cheese-pizza)) (define onions-anchovies-cheese (make-topped-pizza "onions" anchovies-cheese))
  • 6. This data definition is self-referential (define-struct topped-pizza (topping base)) A Topping is a String. A Pizza is either -- the string "plain crust" -- (make-topped-pizza Topping Pizza) We also call this a recursive data definition
  • 7. Template for pizza functions pizza-fn : Pizza -> ?? Given a Pizza, produce .... (define (pizza-fn pizza) (cond [(plain-pizza? pizza) ...] [else (... (topped-pizza-topping pizza) (pizza-fn (topped-pizza-base pizza)))])) Add to wishlist: plain-pizza? : Pizza -> Boolean returns true iff the given pizza is a plain pizza
  • 8. This template is self-referential pizza-fn : Pizza -> ?? Given a Pizza, produce .... (define (pizza-fn pizza) (cond [(plain-pizza? pizza) ...] [else (... (topped-pizza-topping pizza) (pizza-fn (topped-pizza-base pizza)))])) We also call this a recursive template
  • 9. Example: number of toppings number-of-toppings : Pizza -> Number Given a Pizza, produce the number of toppings Examples: (number-of-toppings plain-pizza) = 0 (number-of-toppings cheese-pizza) = 1 (number-of-toppings anchovies-cheese) = 2 (number-of-toppings onions-anchovies-cheese) = 3
  • 10. Example: number of toppings number-of-toppings : Pizza -> Number Given a Pizza, produce the number of toppings Strategy: structural decomposition (define (number-of-toppings pizza) (cond [(plain-pizza? pizza) ...] [else (... (topped-pizza-topping pizza) (number-of-toppings (topped-pizza-base pizza)))])) How many toppings does the plain pizza have? Answer: 0
  • 11. Example: number of toppings number-of-toppings : Pizza -> Number Given a Pizza, produce the number of toppings Strategy: structural decomposition (define (number-of-toppings pizza) (cond [(plain-pizza? pizza) 0] [else (... (topped-pizza-topping pizza) (number-of-toppings (topped-pizza-base pizza)))])) How many toppings does the plain pizza have? Answer: 0
  • 12. Example: number of toppings number-of-toppings : Pizza -> Number Given a Pizza, produce the number of toppings Strategy: structural decomposition (define (number-of-toppings pizza) (cond [(plain-pizza? pizza) 0] [else (... (topped-pizza-topping pizza) (number-of-toppings (topped-pizza-base pizza)))])) Q: If we knew the number of toppings on the pizza base, how many toppings would there on the whole pizza? Answer: one more topping!
  • 13. Example: number of toppings number-of-toppings : Pizza -> Number Given a Pizza, produce the number of toppings (define (number-of-toppings pizza) (cond [(plain-pizza? pizza) 0] [else (+ 1 (number-of-toppings (topped-pizza-base pizza)))])) Self-reference in the data definition leads to self-reference in the template; Self-reference in the template leads to self-reference in the code. Self-reference in the data definition leads to self-reference in the template; Self-reference in the template leads to self-reference in the code. Self-reference in the data definition leads to self-reference in the template; Self-reference in the template leads to self-reference in the code. Self-reference in the data definition leads to self-reference in the template; Self-reference in the template leads to self-reference in the code.
  • 14. Don't forget the tests! ;; plain pizza (check-expect (number-of-toppings plain-pizza) 0) ;; one topping (check-expect (number-of-toppings cheese-pizza) 1) ...etc...
  • 15. Steps for the design strategy What's the answer for the empty pizza? If you knew the answer for the base pizza, how would you get the answer for the whole pizza?
  • 16. Many other examples add-anchovies : Pizza -> Pizza remove-all-anchovies : Pizza -> Pizza Produce a Pizza like the given one, but with all anchovies removed. replace-all-anchovies-with-onions : Pizza -> Pizza replace-all-anchovies : Pizza Topping -> Pizza replace-topping : Pizza Topping Topping -> Pizza Produce a Pizza like the given one, but with all instances of the first topping replaced by the second one.
  • 17. Check the wishlist plain-pizza? : Pizza -> Boolean returns true iff the given pizza is a plain pizza ;; examples: ... ;; strategy: function composition (define (plain-pizza? pizza) (and (string? pizza) (string=? pizza "plain crust"))) ;; tests: ....
  • 18. Summary Represent arbitrary-sized information using a self-referential (or recursive) data definition. Self-reference in the data definition leads to self-reference in the template. Self-reference in the template leads to self-reference in the code. Follow the recipe!

Editor's Notes

  1. Welcome to Week 2, Lesson 2: Recursive Data DefinitionsWe have learned how to represent atomic data, enumeration data (including interval data), and compound data.But all this data is of fixed size. In this lesson we will begin our discussion of how to represent data of variable and unbounded size.
  2. I've written down the purpose statements for two of these functions. Exercise: write down purpose statements for the other three.Another exercise: can you think of other examples of information that might be represented by a self-referential data definition like the one for Pizza?