Day 15 - Variables and loops
Outcomes:
Variables, Mutation, and For Each Loops
1. Introduction (5 minutes)
-
Overview:
- Purpose: if we want to define our own list processing functions, need a way to do computations for each element in
the list. Multiple ways to do that, today we will show iteration with
for each()
. - This requires being able to modify variables, which in Pyret means they must
be declared with
var
, and must be updated with the special operator:=
.
- Purpose: if we want to define our own list processing functions, need a way to do computations for each element in
the list. Multiple ways to do that, today we will show iteration with
-
Motivating Example:
- Let's define our own running total (sum) over a list of numbers.
2. Variables and Mutation in Pyret (15 minutes)
2.1 Declaring and Updating Variables
-
Syntax and Behavior:
- A variable is declared with
var
and given an initial value. - The update operator
:=
changes the value stored in the variable.
- A variable is declared with
-
Example: First Encounter With Variable Mutation
var total = 0
spy: total end
total := 5
spy: total end -
Interactive Exercise:
- Ask: “What do you expect to see if you execute:
?”
var score = 0
score := score + 10
score := score * 2
spy: score end - Invite a few responses and then run the code together.
- Ask: “What do you expect to see if you execute:
2.2 Variable Mutation in Memory
-
Discussion:
- We already understood the program directory that maps names to values.
- When we update a variable with
:=
, the directory’s entry for that variable changes.
-
Diagram Activity:
- Draw a simple diagram on the board showing the variable
total
with its initial value, then how it changes after each update.
- Draw a simple diagram on the board showing the variable
-
Do Now:
- “Trace the updates for the following code on paper:
What is the final value of
var x = 10
x := x + 5
x := x * 2x
?”
- “Trace the updates for the following code on paper:
3. For Each Loops in Pyret (25 minutes)
3.1 Introduction to for each
-
Concept:
- Pyret’s
for each
loop is a general construct that runs a block of code for every element in a list.
- Pyret’s
-
Syntax Overview:
for each(item from some-list):
<body using item added to program directory>
end -
Example – Summing a List:
var total = 0
for each(n from [list: 0, 1, 2, 3]):
total := total + n
end
check:
total is 6
end- Discussion:
- Ask, “What is happening at each iteration? How does
n
change? What is the role of:=
in updatingtotal
?”
- Ask, “What is happening at each iteration? How does
- Discussion:
3.2 More Examples Using for each
-
Example – Calculating a Product:
var product = 1
for each(n from range(1,5)):
product := product * n
end
check:
product is 24
end -
Note: we used library function
range
that produces a list -- try it out in the interactions pane! -
Interactive Exercise:
- Ask students: “Modify the loop to compute the sum of even numbers in the list
[list: 1, 2, 3, 4, 5, 6]
.” - Expected solution (or with
if
and nothing in theelse
branch):var even_sum = 0
for each(n from [list: 1,2,3,4,5,6]):
when num-modulo(n, 2) == 0:
even_sum := even_sum + n
end
end
check:
even_sum is 2 + 4 + 6 # which is 12
end
- Ask students: “Modify the loop to compute the sum of even numbers in the list
3.3 How for each
Integrates with the Directory
-
Discussion:
- Explain that the
for each
loop temporarily introduces a variable (e.g.,n
) that takes on successive elements of the list. - Show with a diagram how:
- Initially,
total
is 0. - On the first iteration,
n
is 0, and thentotal
is updated. - Continue until the loop finishes.
- Initially,
- Explain that the
-
Do Now:
-
“Trace the following code on paper and draw the directory after each iteration:
var counter = 0
for each(word from [list: "hello", "world"]):
counter := counter + 1
endWhat is the final value of
counter
?”Now add a
spy
statement to see the trace as the program runs.var counter = 0
for each(word from [list: "hello", "world"]):
spy: word end
counter := counter + 1
end
-
-
Explain:
- The variable
counter
is updated during each iteration. The temporary variableword
holds each element, but its final value isn’t used outside the loop.
- The variable
4. Wrap-Up (5 minutes)
- Recap Key Points:
- Declaring mutable variables using
var
and updating them with:=
. - Using
for each
to iterate over lists while performing side effects. - How the directory (the mapping of variable names to values) changes during the execution of a loop.
- Declaring mutable variables using
Optional Challenge
- Challenge Exercise:
- Write a program that uses
for each
to compute both the sum and the product of numbers in a list, and then prints both values. - Expected solution:
var sum = 0
var prod = 1
for each(n from [list: 2, 3, 4]):
sum := sum + n
prod := prod * n
end
sum
prod
- Write a program that uses