Prologue: How to Program
#has-images #howto_design_programs_2e

### Prologue: How to Program

When you were a small child, your parents probably taught you to count and later to perform simple calculations with your fingers: “1 + 1 is 2”; “1 + 2 is 3”; and so on. Then they would ask “what’s 3 + 2” and you would count off the fingers of one hand. They programmed, and you computed. And in some way, that’s really all there is to programming and computing.

Start DrRacket and select “Choose language” from the “Language” menu. This brings up a dialog listing “Teaching Languages” for “How to Design Programs” (and possibly other books). Click “Beginning Student Language” and “OK” to set up DrRacket for this chapter.

Now you’re switching roles. You program, and the computer is a child. Of course, you start with the simplest of all calculations. You type

( + 1 1 )

into the top part of DrRacket, click RUN , and a result shows up in the bottom part: 2

That’s how simple programming is. You ask questions as if DrRacket were a child, and DrRacket computes for you. People often say the “computer does X for you” but in reality, a plain computer is pretty useless. It is software that computes. You can also ask DrRacket to process several requests at once:
 ( + 2 2 ) ( * 3 3 ) ( - 4 2 ) ( / 6 2 )
After you click RUN , you see 4 9 2 3 in the bottom half of DrRacket, which are the expected results.

Terminology At this point, we slow down for a moment and introduce some terms:
• The top-half of DrRacket is called the definitions area . In this area, you create the programs, which is called editing . As soon as you add a word or change something in the definitions area, the SAVE button shows up in the top-left corner. When you click SAVE for the first time, DrRacket asks you for the name of a file so that it can store your program for good. Once your definitions area is associated with a file, clicking SAVE ensures that the content of the definitions area is stored safely in the file.

• Programs consist of expressions . You have seen expressions in mathematics. For now, an expression is either a plain number or something that starts with a left parenthesis “(” and ends in a matching right parenthesis “)”—which DrRacket rewards by shading the area between the pair of parentheses.

• When you click RUN , DrRacket evaluates the expressions in the definitions area and shows their result in the interactions area . Then, DrRacket, your faithful servant, awaits your commands at the prompt . The appearance of the prompt signals that DrRacket is waiting for you to enter additional expressions, which it then evaluates like those in the definitions area:
 > ( + 1 1 ) 2
Enter an expression at the prompt, hit the “return” or “enter” key on your keyboard, and watch how DrRacket responds with the result. You can do so as often as you wish:
 > ( + 2 2 ) 4 > (* 3 3 ) 9 > ( - 4 2 ) 2 > ( / 6 2 ) 3 > ( sqr 3 ) 9 > expt 2 3 ) 8 > ( sin 0 ) 0 > ( cos pi ) #i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i#i-1.0
Take a close look at the last number. Its “#i” prefix is short for “I don’t really know the precise number so take that for now” or inexact number . Unlike your calculator or most other programming systems, DrRacket is extremely honest. When it doesn’t know the exact number, it warns you with this special prefix. Later, we shall show you really strange facts about “computer numbers,” and you will then truly appreciate that DrRacket issues such warnings.

Enough terminology for now.

By now, you might be wondering whether DrRacket can add more than two numbers at once, and yes, it can! As a matter of fact, it can do it in two different ways:
 > ( + 2 ( + 3 4 ) ) 9 > ( + 2 3 4 ) 9
The first one is nested arithmetic , as you know it from school. The second one is Racket arithmetic and it is natural, if you always use parentheses to group operations and numbers together. This is as a good a time as any to discuss the nature of our notation—dubbed Beginning Student Language or just BSL —something you might have pondered for a while now.

This book does not teach you Racket, even if the software is called DrRacket. Instead it uses a series of teaching languages created for learning design principles. Once you have mastered these languages, you can quickly learn to program in all kinds of programming languages, including Racket and also JavaScript, Python, Ruby, and others.

In BSL, every time you want to use a “calculator operation,” you write down an opening parenthesis followed by the operation, the numbers on which the operation should work (separated by spaces or even line breaks), and ended by a closing parenthesis. The items following the operation are called the operands . Nested arithmetic means that you can use an expression for an operand, which is why
 > ( + 2 ( + 3 4 ) ) 9
is a fine program. You can do this as often as you wish:
 > ( + 2 ( + ( * 3 3 ) 4 ) ) 15 > ( + 2 ( + ( * 3 ( / 12 4 ) ) 4 ) ) 15 > ( + ( * 5 5 ) ( + ( * 3 ( / 12 4 ) ) 4 ) ) 38
There are no limits to nesting, except for your patience.

Naturally, when DrRacket calculates for you, it uses the rules that you know and love from math. Like you, it can determine the result of an addition only when all the operands are plain numbers. If an operand is a parenthesized operator expression—something that starts with a “(” and an operation—it determines the result of that nested expression first. Unlike you, it never needs to ponder which expression to calculate first—because this first rule is the only rule there is to it.

The price for DrRacket’s convenience is that parentheses have meaning. You, the programmer, must enter all these parentheses, and you may not enter too many. For example, while extra parentheses are acceptable to your math teacher, this is not the case for BSL. The expression ( + ( 1 ) ( 2 ) ) contains way too many parentheses, and DrRacket lets you know in no uncertain terms:
> ( + ( 1 ) ( 2 ) )

function call: expected a function after the open parenthesis, but found a number

Once you get used to BSL programming though, you will see that it isn’t a price at all. First, you get to use operations on several operands at once, if it is natural to do so: Or you place the cursor next to the operation and hit F1. This action opens DrRacket’s HelpDesk and searches for the documentation of the operation. Use the results concerning the HtDP teaching languages. As you may have noticed by now, this text is also linked to the documentation in HelpDesk.
 > ( + 1 2 3 4 5 6 7 8 9 0 ) 45 > ( * 1 2 3 4 5 6 7 8 9 0 ) 0
If you don’t know what an operation does for several operands, enter an example into the interactions area and hit "return"; DrRacket lets you know whether and how it works. Second, when you will read programs that others write—which you will do for about half the time of your programmer life—you will never have to wonder which expressions are evaluated first. The parentheses and the nesting will immediately tell you so.

In this context, to program is to write down comprehensible, arithmetic expressions, and to compute is to determine their value. With DrRacket, it is easy to explore this kind of programming and computing.

If you want to change selection, open document below and click on "Move attachment"

Prologue: How to Program