Unit 3:   Structures and Worlds

imageUnit 3Structures and Worlds
Unit Overview

Students, having worked with pre-made data structures in the last lesson (Cakes), generalize their understanding by defining more data structures of their own, accessing their fields, and writing functions that produce them.

Product Outcomes:
  • Students define a new data structure: a party

  • Students will write functions that access fields of a Cake, and produce new Cakes

Standards and Evidence Statements:

Standards with prefix BS are specific to Bootstrap; others are from the Common Core. Mouse over each standard to see its corresponding evidence statements. Our Standards Document shows which units cover each standard.

    Length: 95 minutes
    Glossary:
    • contract: a statement of the name, domain, and range of a function

    • domain: the type of data that a function expects

    • dot-accessors: A way to extract values from a data structure

    • name: how we refer to a function or value defined in a language (examples: +, *, star, circle)

    • purpose statement: a brief description of what a function does

    • range: the type of data that a function produces

    • variable: something that changes

    Materials:
    • The Party Planner file preloaded on students’ machines

    • Pens/pencils for the students, fresh whiteboard markers for teachers

    • Class poster (List of rules, design recipe, course calendar)

    • Editing environment (Pyret Editor)

    • Language Table

    • The Bakery file used in the previous unit

    Preparation:
    • Seating arrangements: ideally clusters of desks/tables

    Types

    Functions

    Number

    + - * / num-sqr num-sqrt num-expt

    String

    string-append string-length

    Image

    rectangle circle triangle ellipse star text scale rotate put-image

    Cake

    cake .flavor .color .message .layers .is-iceCream


    Review: The Party Struct

    Overview

    Learning Objectives

    • Write complex functions that consume, modify and produce structures

    • Deepen their understanding of structures, constructors and accessors by being introduced to a new data structure.

    Evidence Statements

      Product Outcomes

      • Students define a new data structure: a party

      Materials

      • The Party Planner file preloaded on students’ machines

      • Pens/pencils for the students, fresh whiteboard markers for teachers

      • Class poster (List of rules, design recipe, course calendar)

      • Editing environment (Pyret Editor)

      • Language Table

      Preparation

      • Seating arrangements: ideally clusters of desks/tables

      Review: The Party Struct (Time 10 minutes)

      • In the last lesson you learned about a new kind of data structure, called a Cake. However, a Cake isn’t the only kind of structure - we can create any kind we want! Let’s define another one. In this exercise, you’ll be a party planner. Data structures will be a useful way to represent each Party that you’re planning, keeping track of its location, theme, and number of guests.
        • What datatype could be used to represent the location of a Party?

        • What about the Party’s theme? (This could be something like "50s" or "laser tag".)

        • How about the number of guests?

        Fill out the Party structure definition on Page 13 in your workbook.

          Once the Party structure is defined, you have access to new pieces of code: a function to make a Party, and three dot-accessors to get the location, theme, and number of guests out of the Party.
        • What is the Name of the function that creates a Party?

        • What is the function’s Domain? (What kinds of things are part of a Party?)

        • What is the Range of this function?

        • On Page 13, use the constructor function to define two new Parties of your own.

        • Right below your new Parties, list how you would access the fields of party2. How would you get the location out of party2? (Think about how you got the model or color out of a Car.)

        • Open the Party Planner file. Take a look at the first four lines in the definitions area. Do they match what you have written in your workbook for the data definition of Party?

        • Now define two new Party structures of your own. No matter what party you’re planning, make sure that your party has the right inputs in the right order.

        As with the Cake structure, repetition is key: have students identify the fields of each of their parties, and ask them lots of questions: How would you get the theme out of Halloween? How would you get the number of guests out of JulyFourth?

      Your Bakery

      Overview

      Learning Objectives

      • Students will write complex functions that consume, modify and produce structures

      Evidence Statements

        Product Outcomes

        • Students will write functions that access fields of a Cake, and produce new Cakes

        Materials

        • The Bakery file used in the previous unit

        Preparation

          Your Bakery (Time 80 minutes)

          • In the last lesson you learned about a new data structure, called a Cake.
            • What is a Cake? What information is part of a Cake structure?

            • What function creates a Cake?

            • How do you get the flavor out of a Cake? The message? The color?

            As part of running a bakery, we’ll take orders for Cakes and bake them to the customers specifications. Let’s figure out how to write functions to help us do that.

          • Turn to Page 14 in your workbooks. Write a function called change-flavor, which takes in a Cake and a flavor, and returns a new Cake that is mostly the same as the original, but is now the given flavor.

            • What is the domain for this function? We’ll need to know which Cake we’re taking in AND what flavor it should be.

            • What do you think our bakery is going to give back? What would be the range of change-flavor?

              In your first example, use the original cake1 and make it strawberry. We know our customer will expect to get a Cake back: you wouldn’t return a Cake made incorrectly to a bakery and be OK with only getting a frosting message back! But we won’t be returning the same Cake with our function - we’re making a new Cake, which must have the same message, color, layers, etc. as the given Cake, but one thing needs to be different: the flavor!

            It might not be immediately obvious to students that when a function returns a Cake, they must use the cake constructor function to produce it. This way the function does not return the same value it was given, but instead creates a Cake with the same specs as the given Cake, where only the flavor is different. By starting with a "fresh" Cake, students are forced to think about every single field in order. Thinking about what exactly makes up a Cake and going back to the contract for cake gives them lots of practice with the Cake struct and dot-accessors.

          • The moment you write Cake in the function’s Range, you know that you’ll need to call the cake constructor and give it five things: the flavor, color, message, layers, and is-iceCream of that cake.  

            We already know what flavor this cake should be: strawberry! So we can start by giving our cake function the given String as its first input (since the first input to the cake function must be its flavor).

             
            • We also know what color this Cake should be: the same as the given Cake! So how could you access JUST the color of cake1 and use it in the cake function? Use the dot-accessor! The message of the cake doesn’t change, either. So how do you get the message out of cake1?

            • The number of layers shouldn’t change if you change the flavor. How do you get the layers out of cake1?

            Don’t forget the last field in the Cake struct: is-iceCream! The purpose statement for change-flavor doesn’t say anything about the inside make-up of the Cake changing, so how do you get the original is-iceCream out of cake1?

             

            Remind students that the arguments to each function in Pyret must be separated by commas. This wasn’t necessary in Racket, but they will receive error messages if they don’t use commas correctly in Pyret code.

            • Write one more example for the function change-flavor, this time using it to make cake2 a vanilla cake.

            • Next, circle and label what changes between the two examples. How many variables will this function need? Then write the definition, using your examples to help you.

            After replacing the changing things with variables, your definition should look similar to:  

            Students may be tempted to put new-flavor in quotes, because the flavor of the Cake must be a string. However, the domain of change-flavor tells us that the function will take in a Cake and a String, so whatever flavor is input will already have quotes around it. Values evaluate to themselves, so the string "new-flavor" cannot evaluate to anything other than "new-flavor". If we want new-flavor to be a variable, or shortcut for "strawberry", "vanilla", "chocolate", etc. it must be written WITHOUT quotation marks.

          • Turn to Page 15 in your workbooks. Your bakery needs to know if certain Cakes need to be refrigerated. If the temperature is greater than 32 degrees AND the given cake is an ice cream cake, the function should return true.

            • Fill out the Contract and Purpose Statement for the function.

            • Write two examples for how one would use will-melt.

            • Circle and label what varies between those examples and label it with a variable name.

            • Define the function.

            Give students plenty of time to practice using dot-accessors, extracting pieces of the Cake structures and modifying them.

          Closing

          Overview

          Learning Objectives

            Evidence Statements

              Product Outcomes

                Materials

                  Preparation

                    Closing (Time 5 minutes)

                    • So far you’ve worked with three data structures: Coords, Parties, and Cakes. In the next lesson, you’ll look at a new struct as you dissect a video game, and start thinking about the struct(s) you’ll need to create for your own game!

                      Have students volunteer what they learned in this lesson