Assignment 1 — Flags: names and your first function
Build a flag in Pyret, replace every repeated number with a name so the whole thing scales from one value, then turn it into a function you can call at three different sizes.
Due Sep 1, 2026
This assignment comes in two parts. Part A uses only what we did on Wednesday — you can finish it that evening. Part B uses functions, which we cover on Monday, and we will start it together in class. Do not wait for Monday to begin Part A. It is a good idea to try Part B before Monday as well, once you have done the reading ([DCIC] 3.3).
Everything goes in a single Pyret file. Open https://code.pyret.org/, name the file a1-flags.arr, and keep working in it all week.
Save your work somewhere that is not the classroom computer. A file saved in Sutliff 105 stays on that one machine, and your home folder is per seat — sit elsewhere next class and it is not there. Sign in at code.pyret.org so it saves to your Google Drive, or paste your code into a document of your own before you leave the room. “It was on the lab computer” is not an extension.
AI: the course AI policy applies in full. No AI-generated code, and no asking a chatbot to solve these problems before the deadline. The optional problems at the end say when that changes.
What the shapes take
You are not expected to remember these. Keep this next to you while you work.
circle(radius, mode, color) above(top, bottom)
rectangle(width, height, mode, color) beside(left, right)
square(side, mode, color) overlay(front, back)
triangle(side, mode, color) rotate(degrees, image)mode is "solid" or "outline". above, beside and overlay each take exactly two images — for three, you nest one inside another.
Everything else is in the Pyret image documentation, including the full list of colours and a lot of shapes we have not used. Looking things up there is not cheating; it is the skill.
Part A — names (after Wednesday)
Build a flag. Pick a real one, or invent one. It must use at least three shapes and at least two different shape functions (
rectangle,circle,triangle,star, …), combined withoverlay,aboveorbeside.- Suggested, in rough order of difficulty: Poland, Japan, Armenia, Greece, Bangladesh.
- Write it first the obvious way, with the numbers typed in directly. It does not have to be pretty yet.
Save the “before” picture. Copy that first expression into a comment at the top of your file. You are going to change the code a lot, and it is worth being able to see where you started.
Kill every repeated number. Go through your expression. If a number appears more than once, give it a name with
=and use the name in both places. Same for colours.- When you are done, no number and no colour string should appear twice in the flag expression.
- Names should say what the thing is, not what it is made of:
stripe-height, nottwenty.
UPDATED 2026-09-07 — naming colours. That second rule decides colours too, and it is the one that is easy to get wrong. Name the colour’s job in the flag, not the colour itself.
stripe-color = "crimson"is worth writing: it says what the colour is for, and it stays true if you change your mind about the shade.red = "crimson"is worse than nothing, because it tells a reader “red” when the answer is crimson. Andred = "red"istwenty = 20for colours — the name says exactly what the value already said. A name earns its keep when it tells you something the value cannot.One name to rule them all. Add a name
unitat the top, and define every other measurement in terms of it.unit = 20 flag-width = unit * 3 flag-height = unit * 2Then changing
unit = 20tounit = 60must make the entire flag three times bigger and still correct — same proportions, nothing sticking out, nothing left behind at the old size.- This is the part to actually check. Change
unit, press Run, and look at the picture. If some piece did not grow with the rest, that piece is still holding a bare number. Fix it and check again.
- This is the part to actually check. Change
Part B — your first function (after Monday)
Write a helper function, tests first. Most flags have a proportion rule: the height is two-thirds of the width, or the stripe height is the total height divided by the number of stripes. Your flag has its own rule. Work out what it is, and write that rule as a function.
- Write it in this order: the types of the parameters and the result, then the
doc:string saying what it computes, then thewhere:examples, and only then the body. That order is the design recipe. - At least three examples in the
where:block, and at least one of them should be an awkward case: zero, one, or a number that does not divide evenly. - The name is yours to pick, and so is the rule. Any measurement your flag computes from another one will do.
UPDATED 2026-09-08 — the example below used to be a flag rule, and that was a mistake on my part. It showed a finished
flag-heightfor a 3:2 flag with its examples already filled in, so it read as the answer rather than as an illustration — and it was copied whole onto flags that are not 3:2. Here is the same recipe worked on something with nothing to do with any flag. Copy the shape; the rule, the name and the numbers have to come from the flag you drew.fun square-area(side :: Number) -> Number: doc: "the area of a square with the given side" ... where: square-area(3) is 9 square-area(0) is 0 square-area(1/2) is 1/4 endPyret writes the
where:block after the body, so nobody can see from the file which you typed first — but they can see the difference it makes. Notice that the first example saysis 9, notis 3 * 3. Work the answers out yourself and write the answer down. An example copied out of the body inherits whatever is wrong with the body and then agrees with it, so it only ever checks that you can copy; a9you worked out yourself arrived by a different route, and two routes to the same number can disagree. That is what step 5 is graded on.- Write it in this order: the types of the parameters and the result, then the
Turn your flag into a function. Write
fun flag(unit): ... end, so that the body is the expression from Part A andunitis now the parameter instead of a name defined at the top. Give it a contract and a purpose statement too.Three flags, one expression. Somewhere in your file, produce a picture of three flags of different sizes side by side, using one expression and no copy-paste of the flag body.
Submit
a1-flags.arron Brightspace. One file. Make sure it runs from top to bottom with no errors before you submit — a file that does not run gets no credit for the parts that would have worked.
Grading
Full credit for a file that runs and does 1, 3, 4, 5, 6 and 7.
Two things carry most of the weight, because they are the point of the assignment:
- Step 4 — one change to
unitrescales the whole flag. - Step 5 — the types, the
doc:string and the examples are all there, and the examples are worked-out answers rather than the formula written a second time.
Presentation
One or two of you will be asked on Wednesday, September 2 to put your file on the projector and walk the class through it — what you named, and what broke when you changed unit. You will not be told in advance, and a solution that does not run is not an excuse. See the syllabus on how presentation is graded.
What else to do (optional part)
Ungraded. No points, and skipping them costs nothing.
Add a colour parameter, so you can call
flag(20, "red"). What happens if your flag has more than one colour in it? Say in a comment what you would need in order to handle that properly.Find one image function in the Pyret documentation that we did not use in class, and use it in your flag. In a comment, say where in the documentation you found it.
What does
flag(0.5)do? What aboutflag(-3)? Neither should crash the assignment — just try them and write down in a comment what you saw. One of the two tells you something about how Pyret handles numbers that most languages get wrong.Try to draw an 8×8 checkerboard using only what you know so far. You will not enjoy it. Get as far as you can, then write one sentence about what tool you wish you had. (You will have it in about two weeks.)
After the deadline has passed — and only then, per the AI policy — ask a chatbot to write your flag in Pyret. Paste its answer into a comment. Does it run in CPO? If it does not, write one line saying what is wrong with it. If it does, write one line saying what you like better about yours.