2 The Value(s) of Computing
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
3
"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.
What is this program’s value?Yes, you must put spaces around +.
1 + 2
"will." + "i." + "am"
2.3 Precedence
4 + 2 - 5 + 1
(4 + 2) - (5 + 1)
4 + (2 - 5) + 1
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.”—
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.
check:
(1 + 2) is 3
("will." + "i." + "am") is "will.i.am"
end
If instead you claim:
check:
("will." + "i." + "am") is "william"
end
What will happen?