Students analyze the behavior of a piecewise function
Students learn the concept of piecewise functions
Students learn about conditionals (how to write piecewise functions in code)
Students will understand that functions can perform different computations based on characteristics of their inputs
Students will begin to see how Examples indicate the need for piecewise functions
Students will understand that cond statements capture pairs of questions and answers when coding a piecewise function
Computer for each student (or pair), running WeScheme or DrRacket with the bootstrap-teachpack installed
Student workbooks, and something to write with
Class poster (List of rules, language table, course calendar)
- Luigi’s Pizza
To get started with this lesson, complete Luigi’s Pizza Worksheet.
The code for the cost function is written below:
Up to now, all of the functions you’ve seen have done the same thing to their inputs:
This was evident when going from EXAMPLEs to the function definition: circling what changes essentially gives away the definition, and the number of variables would always match the number of things in the Domain.
green-triangle always made green triangles, no matter what the size was.
safe-left? always compared the input coordinate to -50, no matter what that input was.
update-danger always added or subtracted the same amount
and so on...
Turn to Page 67, fill in the Contract and EXAMPLEs for this function, then circle and label what changes.
It may be worthwhile to have some EXAMPLEs and definitions written on the board, so students can follow along.
The cost function is special, because different toppings can result in totally different expressions being evaluated. If you were to circle everything that changes in the example, you would have the toppings circles and the price. That’s two changeable things, but the Domain of the function only has one thing in it - therefore, we can’t have two variables.
Have students refer back to prior Design Recipe pages - it is essential that they realize that this is a fundamentally new situation, which will require a new technique in the Design Recipe!
Of course, price isn’t really an independent variable, since the price depends entirely on the topping. For example: if the topping is "cheese" the function will simply produce 9.00, if the topping is "pepperoni" the function will simply produce 10.50, and so on. The price is still defined in terms of the topping, and there are four possible toppings - four possible conditions - that the function needs to care about. The cost function makes use of a special language feature called conditionals, which is abbreviated in the code as cond.
- Each conditional has at least one clause. Each clause has a Boolean "question" and a "result". In Luigi’s function, there is a clause for cheese, another for pepperoni, and so on. If the "question" evaluates to true, the "result" expression gets evaluated and returned. If the "question" is false, the computer will skip to the next clause.
Look at the cost function:
How many clauses are there for the cost function?
Identify the question in the first clause.
Identify the question in the second clause.
Square brackets enclose the question and answer for each clause. When students identify the questions, they should find the first expression within the square brackets. There can only be one expression in each answer.
- The last clause in a conditional can be an else clause, which gets evaluated if all the questions are false.
In the cost function, what gets returned if all the questions are false? What would happen if there was no else clause? Try removing that clause from the code and evaluating an unknown topping, and see what happens.
else clauses are best used as a catch-all for cases that you can’t otherwise enumerate. If you can state a precise question for a clause, write the precise question instead of else. For example, if you have a function that does different things depending on whether some variable x is larger than 5, it is better for beginners to write the two questions (> x 5) and (<= x 5) rather than have the second question be else. Explicit questions make it easier to read and maintain programs.
Functions that use conditions are called piecewise functions, because each condition defines a separate piece of the function. Why are piecewise functions useful? Think about the player in your game: you’d like the player to move one way if you hit the "up" key, and another way if you hit the "down" key. Moving up and moving down need two different expressions! Without cond, you could only write a function that always moves the player up, or always moves it down, but not both.
- Right now the else clause produces a String, even though the Range of the function is Number. Do you think this is a problem? Why or why not? As human beings, having output that breaks that contract might not be a problem: we know that the functions will produce the cost of a pizza or an error message. But what if the output of this code didn’t go to humans at all? What if we want to use this function from within some other code? Is it possible that that code might get confused? To find out, uncomment the last line of the program (start cost by removing the semicolon. When you click "Run", the simulator will use cost function to run the cash register. See what happens if you order off the menu!
To fix this, let’s change the else clause to return a really high price. After all, if the customer is willing to pay a million bucks, Luigi will make whatever pizza they want!