How to Design Programs – An Introduction to Programming and Computing

Recommended

Prologue: How To Program

When you were a small child, your parents taught you to count and 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.

Now it is time to switch roles. Start DrRacket. Doing so brings up the window of figure 3. Select “Choose language” from the “Language” menu, which opens a dialog listing “Teaching Languages” for “How to Design Programs.” Choose “Beginning Student” (the Beginning Student Language, or BSL) and click OK to set up DrRacket. With this task completed, you can program, and the DrRacket software becomes the child. Start with the simplest of all calculations. You type

(+ 1 1)

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

That’s how simple programming is. You ask questions as if DrRacket were a child, and DrRacket computes for you. 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.

Let’s slow down for a moment and introduce some words:

  • 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-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 an inexact number. Unlike your calculator or other programming systems, DrRacket is honest. When it doesn’t know the exact number, it warns you with this special prefix. Later, we will show you really strange facts about “computer numbers,” and you will then truly appreciate that DrRacket issues such warnings.

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 BSL arithmetic; and the latter is natural, because in this notation you always use parentheses to group operations and numbers together.

Attribution

Matthias Felleisen, Robert Bruce Findler, Matthew Flatt, Shriram Krishnamurth (2018),How to Design Programs – An Introduction to Programming and Computing, URL: https://win.uantwerpen.be/~sdemey/

This work is licensed under the Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0):
(https://creativecommons.org/licenses/by-nc-nd/4.0/
).

VP Flipbook Maker

Have no idea how to display your work in an interesting way? Visual Paradigm Online is a professional tool for you to convert your work in different format to digital flipbook. You can also choose to create from scratch or customize the templates. Try it now and share your attractive flipbook with others!