Make your own free website on Tripod.com
 

Discussion #2 - Expressions


In Unit #1, you learned that the Logo Interpreter recognizes three kinds of program elements - procedures, expressions, and delimiters - and that there are two types of procedures - commands and reporters. In this Unit, you will learn more about expressions in general and, in particular, about reporters. You recall that commands are procedures that produce an effect, and that reporters are procedures that calculate an output value for a command to use as input. A command together with its inputs is called an instruction. A reporter together with its inputs is one form of expression. The other forms are numerals, quoted words, quoted lists, and dotted words.

When writing an instruction, the command is always typed first, followed by one or more expressions that provide inputs. When writing an expression in the form of a reporter that requires inputs, the reporter is usually typed first, followed by its inputs. For example, you could type the expression random 100 or sqrt 90 in the same way you have typed the instruction fd 100 or rt 90. One group of reporters, however, require that their inputs be separated - one in front of the reporter and one after it. These are the arithmetic operators +, -, *, and / (the * symbol stands for "multiplied by" and the / symbol stands for "divided by.") Separating the inputs this way makes arithmetic expressions look the way you are used to seeing them: "7 + 9" instead of "+ 7 9", for instance. Reporters whose inputs are separated are called infix reporters. (If you are curious to know, reporters whose inputs follow them are called prefix reporters.)

As you will see later, you must be particularly careful not to confuse the Interpreter when you use infix reporters. In Unit #1, you learned that the Interpreter evaluates an expression by determining the value which is to be passed back as input to a command . Most of the commands you have used so far required numbers as inputs. (A few of the scrapbook commands required page names as inputs.) Many of the commands in this unit and the units that follow will require words or lists, as inputs instead of numbers.

Logo words which are used an inputs may contain digits and other symbols as well as letters. The commands that require words as inputs accept single letters, digits, and certain other symbols as well as combinations of these key-board characters. In natural language, we call combinations of letters a "word" and combinations of digits a "numeral", but in Logo both kinds of combinations are called "words."  Commands that require lists as inputs accept groups of Logo words, as defined in the previous sentences.

Even though the Interpreter treats numerals in exactly the same way as it treats words made up of letters, I find it convenient to think of numeral-words and letter-words as separate forms of expressions for two reasons: First, numeral-only expressions do not need to begin with a quotation mark in order to be identified as expressions rather than as procedures. The Interpreter knows that a procedure cannot be written in the form of a numeral, so it automatically considers numerals to be expressions. Second, the Interpreter evaluates numerals by calculating their decimal values. Commands that need numbers as inputs require these values, rather than simply a string of digits. That is the reason I try to make a careful distinction between "numerals" (the symbols) and "numbers" (the calculated values of numerals.)

As I have noted before, there are five forms of expressions that you may use to provide an input to a command. The Interpreter evaluates each form differently. Here is how the Interpreter evaluates each form of expression in preparation for passing its input value to a command: The Interpreter evaluates a numeral by calculating its decimal value and passing back the resulting number. The Interpreter evaluates a quoted word by stripping off the quotation mark (") and passing back the resulting word. The Interpreter evaluates a quoted list of words by stripping off the brackets ([ and ]) and passing back the resulting list. The Interpreter evaluates a reporter with inputs by first evaluating the expressions that provide inputs to the reporter and then calculating the reporter's output value. Depending upon the particular reporter, this value may be a number, a word, or a list. The Interpreter evaluates a dotted word by retrieving and passing back a value that has previously been associated with this word. The value may be a number, a word, or a list. You will learn how to use this form of input in Unit #3.

Whenever it is important for me to distinguish clearly between an expression (a numeral, quoted or dotted word, quoted list, or reporter) and the input value obtained when the expression is evaluated (a number, word, or list), I shall place single quotes around the input value. For example, the expression "true provides the input 'true'; the expression [fd 10 rt 90] provides the input 'fd 10 rt 90'; the expression 2 * 5 provides the input '10'. This use of single quotes is only a visual device to help clarify my explanations. The single quotes are not part of the actual input that is passed to a procedure!
 

Exploration #2

New Commands


repeat number.of.repetitions instruction.list Executes the list of instructions over and over, as many times as required by the first input.

print (pr) number, word, or list Displays the input value on the page, starting at the text cursor's position.

ct (clear text) Erases text that was displayed with the pr command, and returns the text cursor to its home position.

fill Fills a closed figure with the turtle's pen color. The turtle must be located inside the figure, disconnected from the figure's boundary lines, with her pen down. This means that after drawing the figure, the turtle must be moved inside with the mouse or with an instruction list that begins with a pu command and ends with a pd command so that she does not leave a trail connecting her with the figure's boundary line.
 

New Reporters

dividend / divisor
factor * factor
minuend - subtrahend
addend + addend
Calculates the result of the arithmetic expression (division, multiplication, subtraction, or addition in the order listed above.) The inputs, of course, must be numbers. In the case of division, the divisor must not be zero.
 

1. Use each of the arithmetic expressions as the input to pr. Experiment with unusual values for the inputs.

2. Use the repeat command to experiment with drawing "perfect" regular squares, hexagons and octagons. Use the numeral 4, 6, or 8 as the first expression. Use a quoted instruction list [fd x rt y] (where x is length of each side and y is the size of each angle) as the second expression. For example: repeat 4 [fd 50 rt 90] By "perfect," I mean figures that fit on the page and that end up at exactly the same place as they started. When you have drawn a figure that looks exact, you might want to draw an greatly enlarged version by increasing the value of x, just to be sure there is no gap or overlap between the starting and ending points.

3. Try this with with a regular 7-sided figure. Are you sure it's "perfect?"

4. Do the same experiments with figures of 3, 5, 9, and 10 sides. Make a chart of the number of sides and the turning angles. Do you see a pattern? Can you predict from the pattern what the turning angle would be for a 12-sided figure? a 15-sided figure? Try them to see if you are right.

5. Now that you have discovered the special importance of 360-degree turns, known in Logo as the "Total Turtle Trip Theorem," modify your instruction to let the turtle calculate her own turning angle. You can do this by using a reporter instead of a numeral to provide the y input value. For instance: repeat 7 [fd 100 rt 360 / 7] Try this technique to draw other polygons with an unusual number of sides.

6. How do you suppose the turtle would draw a circle? Try to teach her.

7. What do you suppose would result if you made the turtle rotate through two complete circles in order to complete the figure? Experiment with an instruction like this: repeat 5 [fd 100 rt 2 * 360 / 5] To evaluate expressions such as 2 * 360 / 5, the Interpreter does the arithmetic in the same order as you would, from left to right. It first evaluates 2 * 360 to find the first input to /, and then evaluates 720 / 5 to find the input to rt.

8. Experiment with stars that have different numbers of points and that require more than two trips around to complete.

9. A rectangle is not a "regular" figure because its length and its width may be different. Can you use the repeat command when you write a procedure to draw a rectangle? Try it.

10. A rhombus is not a "regular" figure because its adjacent angles may be different. Can you use the repeat command when you write a procedure to draw a rhombus? Try it.

11. Use your experience from the above two problems to write a procedure to draw a parallelogram.

12. How do you suppose the turtle could be taught to draw a trapezoid?