Week 02 Lesson 02 Recursive Data Definitions

604 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
604
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 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.
  • 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?
  • Week 02 Lesson 02 Recursive Data Definitions

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

    ×