Intro to Programming (Time 15 minutes) Open the programming tool of your choice: If you ve installed, DrRacket, double-click the application to launch it. If you are using the online-tool, click here to log in: http://www.wescheme.org/. Enter your username and password to log in, then click "Start a New Program". Have the students look at the editor on their computers. Refer to overhead projector as necessary. This screen is called the editor, and it looks something like the diagram you see here. There are a few buttons at the top, but most of the screen is taken up by two large boxes: the Definitions area on the left and the Interactions area on the right. The Definitions area is where programmers define values and functions in their program, while the Interactions area allows them to experiment with those values and functions. This is analogous to writing a series of function definitions on a blackboard, and having student evaluate expressions using those function on scrap paper. As students are not yet defining values of their own, it is not important that students understand this distinction right now. For now, we will work only with the Interactions area. A program is a legal expression that, when evaluated, produces a value. You ve been writing programs using mathematical expressions since you first learned how to add! Just as in English, there are rules that determine whether a sentence makes sense. Programs have rules too! A program can be very complicated, but it doesn t have to be: the simplest programs of all are just Numbers. Draw students attention to the Language Table (see Lesson Overview for Unit 1), which currently sits empty. Add "Numbers" to the Types section of the language table. Page 16 of 21
Code Rule 1: All values are legal expressions. What do you think 4 will evaluate to? Click here to test it out.: http://www.wescheme.org/openeditor?interactionstext=4& Enter 4 in the Interactions area and hit "Return". You will see the value 4 appear on the next line in the Interactions area. Type 10 in the Interactions area and hit "Return". Now the value 10 appears in the Interactions area. Try evaluating numbers, like 10345017, or negative numbers, like -2. Is there a limit to how big a number can be? What happens if you write a decimal? What happens when you click on a decimal, like 0.75? You get a new type of number, a fraction, like 3/4. The editing environment evaluates all fractions and returns them as decimals by default. This can be surprising to students at first, so you may want to take a moment to explain what s going on, and show them that these decimals can be converted back to fractions just by clicking on them. The environment uses standard annotations for repeating, non-terminating decimal expressions and properly handles expressions like ( 1). If you want to work with those kinds of numbers in your class, enter them to get familiar with how they appear in the Interactions area. The computer obviously knows about Numbers, but what happens if you type in something that it doesn t know about? Will it complain? Crash? Guess? Try asking the computer: http://www.wescheme.org/openeditor?interactionstext=dog& to evaluate dog in the Interactions area. The error message uses the term "variable". Don t worry if your students don t already know this term; we will teach it to them shortly. For now, students just need to get used to error messages and the kinds of problems that they catch in programs. These error messages are really useful for programmers. Rather than saying "this program doesn t work", the computer does the best it can to tell you what went wrong, and to give you as much information as possible to help you fix the problem. Make sure you always read these messages carefully! Page 17 of 21
The Circles of Evaluation are also easy to convert into computer programs. To translate a Circle of Evaluation into a program, begin with an open parenthesis (, and then the function written at the top of the circle. Then translate the inputs from left to right in the same way, adding a closing parenthesis ) when you re done. This process gives us the second rule for expressions: Code Rule 2: Each open parenthesis is followed by one function, then by one or more legal expressions, and finally by a closing parenthesis. Here is the code for this Circle of Evaluation: (- 4 5) Go to the editor: http://www.wescheme.org/openeditor? interactionstext=(- 4 5)&, and type (- 4 5) in the Interactions Area. Press the Return key to evaluate the program. You should see -1 as an answer. Have students practice converting simple Circles of Evaluation into code. If you want to help students understand when to use the parentheses, here are two explanations that we find useful. First, the parens look like the Circle, and the Circle encloses the function name and its inputs. Second, we use a more visual description of an ant eating its way through the expression. The ant eats into the Circle (an open paren), then goes to the function at the top, then to the arguments from left to right, then finally out of the Circle (a close paren). If the ant encounters another Circle while writing down the arguments, it needs another open paren, etc. When a Circle of Evaluation has other circles inside of it, the translation still follows the same rules: each Circle requires a new set of parentheses: (* 6 (+ 4 5)) Try entering this code into the Interactions area. What should the program evaluate to when you hit Return? Practice converting other Circles of Evaluation you ve drawn into code. [Video on Roles in Pair Programming.] Scaffolding for Pair Programming: Talk to students about the roles of Driver and Navigator. The Driver is the student with their hands on the keyboard - they re in charge of typing, using the mouse, etc. The Navigator should be telling the Driver what to type, and pointing out mistakes or suggesting things. It s important to clarify what these roles are (perhaps reinforcing them with a visual), to ensure that both partners are active and talking to one another about the task at hand. The linked video does a great job discussing these roles. Page 18 of 21
All of the expressions that follow the function name are called arguments to the function. The following diagram summarizes the shape of an expression that uses a function. For practice, turn to Page 5 in your workbook. For each mathematical expression, draw the Circle of Evaluation, then convert that Circle into Racket code. Page 19 of 21
When expressions don t follow the code rules, the computer will tell you that it found a problem. The computer also gives you information to help you fix the problem. This information is called an error message. We ll talk more about error messages later. For now, we just want you to see a couple of error messages so that you ll know what they are if you run into one while programming. Enter each of the following illegal expressions in the Interactions area and look at the error message or behavior that the computer gives you. (5 * 6) [puts the function in the middle, instead of at the front] (*5 6) [missing a space after the function] * 5 6) [forgets the open parenthesis] (* 5 6 [forgets the close parenthesis] At this point, the goal is simply to have students see error messages. Students are not expected to be experts at reading error messages this early (you will drill this later). NOTE: When a close parenthesis is missing, hitting return will do nothing, because the computer is waiting for the parenthesis. When an open parenthesis is missing, WeScheme does nothing and waits for the programmer to add the parens. If students hit enter and "nothing happens", they should check their parentheses. Also note that if you do this sequence of exercises with + instead of *, the error appears differently, because +5 is a perfectly valid number (just like -5). Page 20 of 21