Conditionals and Booleans
1 How Programs with Helpers Evaluate
2 Reviewing Conditionals and Booleans

Conditionals and Booleans

Copyright (c) 2017 Kathi Fisler

1 How Programs with Helpers Evaluate

Look at this complete file of the pen-cost programs. The file has functions to compute the cost of the pens, add shipping charges, compute the cost with shipping, and to give a bulk discount on large orders. (We haven’t yet seen the discount function in class).

Imagine that we enter pen-cost(10, "smile") at the prompt. How does Pyret evaluate this expression? It finds the definition of pen-cost, copies the function body, substitutes the arguments for the parameters, then evaluates the copied body.

Imagine that we enter add-shipping(pen-cost(10, "smile")) at the prompt. Pyret evaluates inside-out – it uses the substitution method to obtain the result of pen-cost(10, "smile"), which it then substitutes for order-amt in the body of add-shipping.

Some of you have asked about whether you can—or need to—re-use parameter names across different functions. Let’s step back and think about how substitution works. When a function gets called, Pyret replaces the parameter names with the input values. This means that when the function body gets evaluated, the parameter names are no longer visible. As a result, parameter names can’t be confused or referenced across different functions.

Watch the lecture capture to see this demonstrated step-by-step.

2 Reviewing Conditionals and Booleans

Today’s goal was to do another example of if expressions and booleans. We looked at one problem had everyone sketch a solution, and then looked at three different solutions: one used nested-if expressions, one used a combination of if expressions and booleans, and one used only boolean operations.

Here is the code file with the problem statement and the three solutions we went over.

The discussion of the different solutions is best reviewed by watching the lecture capture.