Class 7 — Wednesday, September 16
The first half is a practice run of Progress Exam 1. It is not marked and nothing about it reaches your grade.
The second half is new material, and none of it is on the exam. var, := and for each arrive today and wait until after Monday.
Outline
- Practice exam — forty minutes, on your own
- Mark your own — answers on the screen
- A name that changes — no keyboards
- A name that can change —
varand:= - Write
count-outlinedagain — pairs, the same function a second way - What a loop gives back
1. Practice exam
Open the starter file from Brightspace and work on your own for forty minutes. Same shape as Monday’s exam, same length of work, real problems.
Open book: this site, your own files, the textbook, Pyret’s error messages. No AI, no talking — the point is to find out now what you cannot do yet, while there is still a weekend in the way.
How Pyret runs your program and how to design functions are the two pages worth having open. Rules 1 to 7 are the exam; rules 8 to 10 are this afternoon.
2. Mark your own
Answers go on the screen and you mark your own paper. Nobody collects anything.
The problems and the answers are both on Practice Exam 1, so you can sit the whole thing again from a blank file this weekend.
Write down the one you could not start. That sentence is the whole point of the morning, and it is what your revision this weekend should be about.
Break after this
3. A name that changes
No keyboards for this step or the next one.
For four weeks a name has meant one value, forever. Pyret has been strict about it — this is an error, not a re-assignment:
x = 5
x = 6Predict first: what does Pyret say? Then we run it.
4. A name that can change
var makes a name that is allowed to change, and := changes it.
var running = 0
running := running + 5
running := running + 1= and := are different operations and Pyret will not let you swap them. A name made with = cannot be assigned to; a name made with var still uses = on the line that creates it.
Now a list walk that uses both:
fun sum-loop(l :: List<Number>) -> Number block:
doc: "add up every number in the list"
var total = 0
for each(n from l):
total := total + n
end
total
where:
sum-loop([list: ]) is 0
sum-loop([list: 5, 1, 7, 3]) is 16
endThree things in that function are new:
block:on the header. A function body of more than one expression has to say so. Every function you have written until now was a single expression.for each(n from l):runs the body once for every element, withnstanding for that element each time round.totalon the line afterend. The loop does not give back the answer. You put the answer somewhere and then you name it.
How Pyret runs your program now has rules 8, 9 and 10 for these, and traces this loop one directory at a time.
5. Write count-outlined again
Pairs. You will both get a turn at the keyboard — I will say when to switch.
Open your a3-lists.arr. count-outlined is already in it, written with cases on Monday. Leave that one alone and write a second one underneath:
fun count-outlined-loop(units :: List<Number>) -> Number block:
doc: "how many of these units stripe draws as an outline"
...
where:
...
endSame answers as the one above it, built the other way: a var, a for each, and an if inside the loop.
An if always needs its else, even here. When there is nothing to do in the other case, the branch is the word nothing:
if ...:
count := count + 1
else:
nothing
endDone looks like: both functions green, and count-outlined-loop([list: ]) is 0 in the where: block.
Copy the where: block off your Monday version — the same examples have to pass. If Monday’s is missing an example at exactly 10, add it to both.
If you finish early, write it a third way in one line using filter and length. Then count the lines in all three and say which you would rather read.
6. What a loop gives back
Predict first, then run it:
answer = for each(n from [list: 1, 2, 3]):
n * 100
end
answerThen your Journal on Brightspace:
You have now written
count-outlinedtwo ways, withcasesand with a loop. Which of the two would you rather be handed by somebody else, and why? Is that the same one you would rather write?
Then paste a3-lists.arr into the Journal — both of you, not just whoever was typing. Your file stays on this machine.