Class 6 — Monday, September 14

Every list is empty, or a first and a rest — trace my-len, then write it, then write A3’s count-outlined.

[DCIC] 5.1 — From Tables to Lists and [DCIC] 5.2 — Processing Lists were due before today.

Quiz 2 runs twice on Brightspace, once now and once at the end of class. The higher attempt counts.

A3 is due tomorrow night. Today is the last class before it.

Outline

  1. Starting code — your A3 file, not a new one
  2. Every list is empty, or a first and a rest — no keyboards
  3. Trace my-len by hand — nobody runs anything
  4. Write my-len — pairs from here on, examples first
  5. The shape every list function has
  6. Count them by hand — the same walk, ending in a number
  7. Paste your file into the Journal — last two minutes, both of you

1. Starting code

Open your a3-lists.arr from Wednesday. Today’s work goes straight into it.

No file yet? A fresh Pyret session is all you need — nothing today comes from a library. Catch the rest up tonight.

2. Every list is empty, or a first and a rest

No keyboards for this step or the next one.

Every list you can write down is one of exactly two shapes:

[list: ]                  # empty
[list: 4, 10, 20]         # a first (4) and a rest ([list: 10, 20])

The rest of [list: 20] is [list: ]. The empty list is not a special case bolted on — it is where every list ends.

Two shapes of data, two branches:

cases (List) units:
  | empty => ...
  | link(first, rest) => ...
end

It is an if with the question already asked for you.

3. Trace my-len by hand

The rules from Class 4, on how Pyret runs your program, plus rule 7 for cases — and that page now works this trace through line by line.

One line per step, each line the one above it with one substitution made. Stop when you have a number.

fun my-len(l :: List<Number>) -> Number:
  doc: "how many elements are in this list"
  cases (List) l:
    | empty => 0
    | link(f, r) => 1 + my-len(r)
  end
end

my-len([list: 4, 10])

Paper or a Journal note, whichever you prefer.

4. Write my-len

Pairs from here to the end of class. You will both get a turn at the keyboard — I will say when to switch.

Type it yourself, and the where: block comes first:

fun my-len(l :: List<Number>) -> Number:
  doc: "how many elements are in this list"
  ...
where:
  ...
end

Three examples before the body, and the first one is the empty list. Then the body. It is four lines.

Done looks like: a green test report, and my-len([list: ]) is 0 as the first line of the where: block.

Break after this

5. The shape every list function has

Most list-processing function will get the same template:

cases (List) l:
  | empty      => # the answer when there is nothing
  | link(f, r) => # combine f with the answer for r
end

The only two decisions are what empty gives back and how f combines.

6. Count them by hand

my-len gave back how many. This one gives back how many of a kind.

fun count-outlined(units :: List<Number>) -> Number:
  doc: "how many of these units stripe draws as an outline"
  ...
where:
  ...
end

stripe outlines anything from 10 up. That is the if you wrote last week.

No library function that walks the list for you — not filter, not length. Use cases.

Done looks like: it runs, and count-outlined([list: ]) is 0 is in the where: block.

Stuck on the body? A where: block with the right answers in it is worth having on its own. Write those and leave the body for tonight.

If you finish early, write all-beside: it takes a list of images and puts them side by side as one image, using cases. It is the same walk over pictures instead of numbers, and the question worth more than the code is what it should give back for [list: ].

7. Paste your file into the Journal

Select all, copy, and paste it into your Journal on Brightspace — both of you, not just whoever was typing. Your file stays on this machine. Sit somewhere else next time and it is not there.

Getting your work off the lab machine has the Google Drive option if you would rather have that as well.