Class 4 — Wednesday, September 2

if, and the examples that pin it down — reducing by hand, then at the keyboard.

[DCIC] 3.4 — Conditionals and Booleans goes out today and is due before class on Wednesday 09/09 — nothing below assumes you have read it, and conditionals start here, from nothing. Three sections are named by number, so that when you do read it you find it describing what your own hand already did.

  1. Reduce an if by hand — on paper or in a Journal note, and you commit to a prediction before anyone runs anything
  2. Type stripe and check your drawing — pairs from here on
  3. Four examples for stripe, before the body — including the one at u = 10, where < and <= disagree
  4. w inside stripe, and w outside it — predict, then run
  5. Start problem 2, the else if — the only class time you get on A2
  6. Paste a2-stripes.arr into the Journal — last two minutes, both of you

1. Reduce an if by hand

Do not run this program. The point is the steps in between, and running it only ever shows you the last one.

unit = 20

fun stripe(u, color):
  if u < 10:
    rectangle(u * 6, u, "solid", color)
  else:
    rectangle(u * 6, u, "outline", color)
  end
end

beside(stripe(4, "red"), stripe(unit, "red"))
What if actually does — read this before you start

This is new today, and you are not expected to have read anything about it yet.

A question like 3 < 2 is not a special kind of thing. It is an expression, it has an answer, and the answer is a value you can write down like any other: true or false. An if works the question out first, the answer takes the question’s place, and only then is one branch chosen and the other thrown away unevaluated.

All three steps, on a program that has nothing to do with ours:

if 3 < 2: "yes" else: "no" end
if false: "yes" else: "no" end
"no"

Your trace needs lines of that middle shape in it. Jumping straight to the branch you can see is right gets today’s answer without the step that says why it is right — and section 3 turns on that step, where you have to say what happens at u = 10 and looking at the picture will not tell you.

On paper: fold your sheet. On the left, the directory once Run has finished. On the right, every expression Pyret goes through, one line per step, from the last line down to a picture. There are more than six.

At the bottom, draw what you think appears. You find out in section 2.

Or type it into a Journal note instead

If you would rather type than write, open your Journal on Brightspace and do the reduction there.

Copy the last line, paste it underneath, and change only what Pyret would have changed. Every step is the line above it with one substitution made, so pasting and editing is both faster than retyping and harder to cheat at — the steps you would have skipped are the ones you now have to type.

Two things work differently than on paper. Put the directory at the top rather than off to the side, since there is nothing to fold. And finish with a sentence describing the picture you expect — how many rectangles, which are solid, which is bigger — instead of drawing it.

It also stays in your Journal, which is worth something: this trace is A2 problem 1 in slow motion, and you will want it over the weekend.

The rules you are applying are on How Pyret runs your program, and there are six of them now — the sixth is the one for if. [DCIC] 3.4 — Conditionals and Booleans §3.4.5 does this same reduction step by step on a different program.

2. Type stripe and check your drawing

Pairs from here to the end of class. You will both get a turn at the keyboard — I will say when to switch, so you do not have to watch the clock.

The same four lines are on the A2 page under Starting code. Copy them from there rather than retyping them off the board, into a fresh file called a2-stripes.arr.

Run it, then look at your paper. Is that the picture you drew?

If it is not, find which line of your trace was wrong. Fixing the drawing is not the point; finding the step is.

If you typed stripe(4, "red") into the left-hand pane and saw nothing, that is not a bug. Defining a function puts its code in the directory and displays nothing. Calls that you want to see go in the right-hand pane — or on their own line at the bottom of the file, which is where the beside(...) line is.

Look at the two stripes. The small one is filled in and the big one is a rectangle you can see through, and that is the whole idea of the day: an outline drawn on a four-pixel stripe has nothing left to show you. Below a certain size you have to draw the thing differently.

3. Four examples for stripe, before the body

This is the spec. It is also A2 problem 1, so what you write here is work you keep.

stripe(u, color) -> Image
below u = 10 it is solid; from 10 up it is an outline

Write four examples, before you touch the body. Two on each side of the rule, so both branches are actually tested — and one of them has to be u = 10 itself.

Work each answer out yourself and write the answer down. An example copied from the body agrees with the body no matter what the body says, so it cannot catch a mistake.

An example here is an image, which is legal and probably not what you expect:

where:
  stripe(10, "red") is rectangle(60, 10, "outline", "red")
end

Done looks like: the test report says four passed, and your four examples are not all on the same side of 10.

Then break it on purpose. Change < to <= in the body and press Run. Exactly one example should go red. If none of them does, your examples are not doing their job yet — fix that before you go on, and change it back afterwards.

Why 10 and not some other number: it is the boundary, and it is the one place < and <= disagree. [DCIC] 3.4 — Conditionals and Booleans §3.4.1 is where that way of choosing examples comes from.

4. w inside stripe, and w outside it

Nothing to type here — we run this one together, on the projector. Predict first.

You wrote u * 6 twice. If that proportion ever changes you have to remember both places, so give it a name:

fun stripe(u, color):
  w = u * 6
  if u < 10:
    rectangle(w, u, "solid", color)
  else:
    rectangle(w, u, "outline", color)
  end
end

stripe(20, "red")
w

Look at the last line. What does w do? Three options: 120, an error, or nothing at all. Decide before we run it.

It is an error: w is unbound.

There is not one directory. There is the big one, and every call makes its own little private one that disappears when the call finishes. w was in the little one.

That is not an accident of the design, it is the reason for it: if w leaked out, every function you ever import could scribble names into yours.

This is [DCIC] 3.4 — Conditionals and Booleans §3.4.6.2, near the end of the section and easy to miss on a first read.

5. Start A2 problem 2, the else if

A2 is due Tuesday and there is no class between now and then, so this is the only stretch of it you get to do with someone to ask.

Get problem 2 working while I am here — the else if. Three cases instead of two: below 10 solid, from 10 up to 30 an outline, and from 30 up something visibly different again. It is [DCIC] 3.4 — Conditionals and Booleans §3.4.4 applied to the function already on your screen, so the only new thing is the syntax.

Write it as one if / else if / else, not as two separate if expressions one after the other.

  • If you finish, go on to problem 3, the Boolean parameter. That is the hardest part of A2 and the best use of the time left.
  • If you are stuck, getting problem 1 running is enough for today. The rest of the assignment builds on it, so that is the piece to leave class with.

After today, Discord #help is the class. A question asked Thursday is worth a great deal more than the same question asked Monday night.

6. Paste a2-stripes.arr into the Journal

These computers keep nothing between sessions.

Select all, copy, and paste it into your Journal on Brightspace — both of you, not just whoever was typing.

If you would rather have Pyret save to your Google Drive, that is on Getting your work off the lab machine. Drive is an offer; the Journal paste is the instruction.