On this page:
2.1 Simple Programs
2.2 Less Simple Programs
2.3 Precedence
2.4 Predicting the Value of Expressions

2 The Value(s) of Computing

    2.1 Simple Programs

    2.2 Less Simple Programs

    2.3 Precedence

    2.4 Predicting the Value of Expressions

Programs exist to compute answers, which we will call values. These values will represent all sorts of interesting phenomena: they may be a frame from the next hit movie, a list of Web pages matching your query, a confirmation code for an upcoming trip, or a prediction of tomorrow’s temperature. Ultimately, they’re all values and our goal here will be to learn how to write programs that compute them.

2.1 Simple Programs

The simplest programs are ones whose value we already know.What use is it if we already know it? You’ll find out later: Predicting the Value of Expressions. Here, then, is the simplest program you can write:

3

If you ask Pyret for the value of this program, it will unsurprisingly tell you that it’s 3. Programs can also refer to text, called a string:

"Hello, Tara!"

The value of this program, likewise, is "Hello, Tara!".

Okay, so we’ve seen numbers and strings. There are many more kinds of values in Pyret, including images. We’ll get to them soon.

2.2 Less Simple Programs

Obviously, we’re not very interested in writing programs whose answers we already know. Therefore, let’s start the first real step of our journey: writing expressions that take one or more steps to produce an answer.

Do Now!

What is this program’s value?Yes, you must put spaces around +.

1 + 2

As you might have guessed, this program’s value is 3. In your head, you probably applied the rules of algebra to compute this program’s value; in fact, Pyret follows the same rules. Everything you know about how to compute the answer of an algebraic expression applies here! Similarly, once I tell you that using + on string concatenates them, you can easily see that

"will." + "i." + "am"

evaluates to "will.i.am".

2.3 Precedence

If you try to write expressions that combine operators, such as

4 + 2 - 5 + 1

Pyret wants you to be clearer about your intent: did you mean for this to evaluate as

(4 + 2) - (5 + 1)

(i.e., 0), or

4 + (2 - 5) + 1

(i.e., 2), or ...? Instead, Pyret simply asks that you parenthesize expressions that combine operators, so either of the interpretations given above is a valid Pyret program.

2.4 Predicting the Value of Expressions

The ambiguous interpretation example points us to a subtle problem with programming: how can we trust the output of a program? Usually our programs will be much larger than these; they might run for hours and days; and at the end, they spit out a value. How can we have any confidence in this value before we go off to make decisions that rely on its correctness? This is our first brush with predictability.“Be careful about reading health books. You may die of a misprint.”—attributed to Mark Twain

There is no magic recipe for this, but there is a great deal we can do to both gain confidence in our programs and learn when we should lose confidence in one. The simplest thing you can do is write down, in the program itself, your prediction of what it should produce: that is, write down its expected answer. If you do this for a variety of smaller inputs, and you’ve thought hard about covering the different kinds of small inputs, then you can have significant confidence that the program works on large inputs as well. This is called testing. We will teach you much more about this and other methods as we progress through this book.

First, let’s learn Pyret’s mechanics for writing tests. You tell Pyret to check your claims:The parentheses are only necessary because you’re using both + and is. As you’ll see later, most of the time we won’t need parentheses around the computation.

check:

  (1 + 2) is 3

  ("will." + "i." + "am") is "will.i.am"

end

Every time you run a program with this code, Pyret will check to ensure these claims are still true.
Do Now!

If instead you claim:

check:

  ("will." + "i." + "am") is "william"

end

What will happen?

Pyret will report an error, informing you that "will.i.am" (the value produced by the expression) did not match "william" (the value you told it to expect).