Structuring and Evaluating Code
1 The Jamaican Flag
2 Code Structure:   Emoji code
3 How Programs Evaluate:   Ice Cream
3.1 What does the Run button do?
3.2 Errors
4 Expressions versus Statements
4.1 Key Takeaways

Structuring and Evaluating Code

Copyright (c) 2017 Kathi Fisler

1 The Jamaican Flag

We started by having everyone put up their solutions to the Jamaican flag, so we could see the various approaches. The ideas raised included:

2 Code Structure: Emoji code

We looked at two versions of a program to draw an emoji face. Our discussion focused on the following issues:

3 How Programs Evaluate: Ice Cream

Imagine that you had the following contents in the definitions window (the one on the left):

  include image

  

  SCOOP-SIZE = 15

  

  cone = flip-vertical(triangle(SCOOP-SIZE * 2, "solid", "tan"))

  

  overlay-xy(circle(SCOOP-SIZE, "solid", "pink"),

    0, 25,

    overlay-xy(circle(SCOOP-SIZE, "solid", "green"),

      0, 25,

      cone))

How will Pyret evaluate this program?

You may have noticed that SCOOP-SIZE is all in caps, while cone is in lowercase. Conceptually, SCOOP-SIZE is a key concept in the program (which we plan to use multiple times), whereas cone is just naming an intermediate computation for readability. We use this naming convention to help us remember the respective roles of the names in the program.

3.1 What does the Run button do?

When you press the Run button, Pyret erases the dictionary then re-processes the definitions window from the beginning. Any dictionary entries that you made only in the interactions (right) window disappear.

3.2 Errors

We discussed error messages and what they might tell us about the order of how Pyret processes code. Pyret actually processes your code in two passes: in the first pass, Pyret checks whether any names would be used before they are added to the dictionary, but it doesn’t perform any computations or actually create any dictionary entries. If all the referenced names would be properly defined, then Pyret runs the expressions from top to bottom.

4 Expressions versus Statements

We’ve already seen that there’s a difference between

  SCOOP-SIZE = 15

  

  SCOOP-SIZE * 2

and their impact on the interactions window: the first adds to the dictionary, while the second displays a value. A piece of a program that changes how future expressions will evaluate is called a statement. Creating a name meets this definition, since expressions will yield errors or not depending on whether a name appears in the dictionary. Expressions perform computations without changing the information that Pyret maintains for running future expressions.

This distinction will become more meaningful later in the course, but we’ll use the terminology to get used to it now.

4.1 Key Takeaways

The theme of this segment is how code can reflect the structure of data or computations. The structure of an image is reflected in which smaller images you use to create it; the structure of a computation is reflected in which operations you choose and how you nest them. Computer scientists look for structure in data and problems, then capture that structure in code so others can see what structure they had in mind.