- Commanding a Turtle
- Adding New Commands
- Iteration & Animation
- Hierarchical Structure
- Procedure Inputs
- Operators & Expressions
- Defining Operators
- Words & Sentences
- User Interface Events
- What If? (Predicates)
- Local Variables
- Global Variables
- Word/Sentence Iteration
- Mastermind Project
- Turtles As Actors
- File Input/Output
- A Java Program
- What's a Class?
- Extending Existing Classes
- Turtle Graphics
- Control Flow
- User Interface Events
- What Is TG?
- TG Directives
- jLogo Primitives
- TG Editor
- Java Tables
- Example Programs
- Installation Notes
- December 13, 2008
- January 6, 2012
- March 15, 2013
- January 20, 2014
- February 13, 2014
- July 29, 2014
- January 18, 2016
- January 29, 2016
Words and Sentences
In the previous lesson, we introduced defining your own operators. You now know how to write procedures that produce an output.
In this lesson, you will learn
- about new values, symbolic values, that you can have in your programs - words,
- a way to group words and/or numbers - sentences, and
- the use of sentences to output multiple things from operators you define, and
- how to draw text (labels) of various sizes on the graphics canvas.
Symbolic Values - Words
So far you have only been working with numeric data. You have been using numbers for lots of things, e.g. the distance the turtle should move, a color number, a number of degrees to rotate the turtle, etc...
There is a lot more to programming than generating numbers and drawing stuff. Logo is a symbolic programming language, a decendent of LISP. Symbolic programming was created to help get computers to do human-like things - the study of artificial intelligence.
So, how do you represent a word in Logo?
When a Logo interpreter sees a double-quote character, i.e. ("), the collection process for characters composing the word starts. Characters are considered part of the word and collected until one of four things happens:
- a space is encountered,
- the end of the sentence the word is part of is encountered,
- a semicolon is encountered (the start of a comment), or
- the end of the current line is encountered.
"Logo is a word made up of the characters: L, o, g, and o. Note that the double-quote introduces the word - it is not part of the word.
You can display words just like numbers. Figure 10.1 shows examples of printing words, note that numbers may also be entered as words. You can even do arithmetic operations on words as long as they are numbers.
Lists of Words - Sentences
In Logo, a sentence is a list of words. Just like a word can be included in a program by quoting it (i.e. "symbol is a quoted word), a sentence can be included in a program by surrounding a word or words with square brackets. You have been using sentences since the repeat command was introduced in the iteration lesson. We've been supplying sentences composed of Logo instructions as its second input. As an example:
Here are a few, more general, example sentences:
Note that Logo's sentences are just lists of words - they do not have grammatic structure.
The second example shows how we might use a sentence to represent an association, a color name followed by its color number. The third example uses a sentence to represent a point, an x,y pair of coordinates.
An important feature of a sentence is that although it can be made up of many words, it is still only one thing. This is very important when a sentence is used as an input to a procedure. Figure 10.2 shows a sentence as an input to println and then shows what happens when you attempt to invoke println with multiple words.
So, even though the sentence [one two three four five] is composed of five words, println displays it properly. println expected one input and so it consumed one thing - a sentence. Alternatively, when I tried to give println two individual words, the interpreter complained because println only consumed one input. Thus, the Logo interpreter expected the second word to be a new command and "words is not a command.
Sentences Can Be Used to Represent Compound Data
Sentences can be output from procedures when compound data makes sense. The example of a point above, [200 50], is useful as a way of representing a position in TurtleSpace. Table 10.1 introduces the pos operator.
|POS||Outputs the current coordinates of the turtle as a sentence. The X coordinate is the first word of the sentence; the Y coordinate is the second (and last) word of the sentence.|
Figure 10.3 demonstrates that pos outputs a sentence, two words, two numbers in this case.
WORD and SENTENCE Operators
Words and sentences can also be assembled using a couple of primitive Logo operators.
The Logo primitive operators for building words and sentences, also known as constructors, are the operators word and sentence, respectively. A word can be built out of multiple words; a sentence can be built by combining multiple words and/or sentences.
Table 10.2 introduces the new word and sentence operators we'll work with for the rest of this lesson.
|Outputs a sentence consisting of its first input concatenated with its second input.|
|WORD||word1 word2||Outputs a word consisting of its first input concatenated with its second input.|
Figure 10.4 shows a few examples of uses of these operators. Notice how flexible the sentence operator is; it can be used to combine a word and a sentence or combine two sentences into one.
But, also note that the word operator only combines two words into a single word. It's not as flexible as the sentence operator. You can't use word to combine a word and a sentence to form a new word, even if the sentence consists of a single word. Figure 10.5 shows what will happen if you try to do this.
Here is the TG applet. Spend a little time playing with the word and sentence operators.
TG Programming Environment Applet
WORD and SENTENCE Expressions
A few lessons ago, you learned about combining operators forming expressions, and plumbing diagrams. All of the examples given involved numeric operations. Word and sentence operators can be chained together, mixed and matched, just like the mathematical operations. Here is an instruction that includes a chain of WORD operations.
Figure 10.6 shows a plumbing diagram of this instruction.
Type it into the TG applet above or the TG application running on your computer to see what it does.
Since, as mentioned above, numbers are just a special cases of words, computations can be part of the construction of words and sentences. Here is a procedure that takes two numbers as inputs and prints a sentence showing the addition of them.
Draw a plumbing diagram of the println instruction, the body of the procedure. Open up the Editor in TG and type in the definition of showAddTwoNumbers. Play with it; invoke it with different inputs in the CommandCenter. Does it do what you thought it would do?
TG's Logo interpreter can also accept a variable number of inputs for the word and sentence operators. You do this by surrounding the operator name and all of the inputs to it within parenthesis. This saves typing and makes the instruction easier to understand. Here is what the examples above look like when the variable number of inputs syntax is employed.
That's about it for an introduction to words and sentences. We'll look at them in more depth in the Word and Sentence Iteration lesson. But we have learned enough to use them in programs; time to see how.
SETPOS: Moving to a Point in TurtleSpace
As mentioned above, there is a primitive operator in Logo which output's the turtle's position as a sentence (pos); and, as you might expect, there is a primitive command that moves the turtle to a specified position. setpos moves the turtle to an absolute position specified by a sentence containing two numbers. The two numbers are a new horizontal (X) coordinate (the first word of the sentence) and a new vertical (Y) coordinate (the second/last word of the sentence).
|SETPOS||sentence||Moves the turtle to an absolute position, specified by a sentence containing two numbers. The first number is a new horizontal (X) coordinate and second/last number is a new vertical (Y) coordinate.|
Go back up to the TG applet and play with the setpos command. Open up the graphics window with the right mouse button, and use the sentence operator to construct inputs for setpos commands. Here is a cool instruction to try out.
Project: More Grid Expressions
In the last lesson, in the rules I layed out for the Grid Toolkit, I mentioned that cells in the grid could be identified by their row and column numbers.
A cell can be identified in one of two ways: 1. by its row and column numbers. Rows are ordered from top to bottom, with the top row numbered zero. Columns are ordered left to right with the leftmost column numbered zero. ...
But in the code I gave you, I only provided procedures to move the turtle to a cell or draw a filled-in cell, with the cell identified by its index. I wanted to wait until after I introduced sentences to provide procedures which do things in a row,column manner.
So you now know how to construct sentences. Words and sentences can also be disassembled using a few primitive Logo operators. Logo has many operators for manipulating words and sentences, but let's initially limit ourselves to two of them: first and last . These operators are commonly referred to as selectors; they select pieces of words and sentences. I will cover the topic of word and sentence selectors more comprehensively in a future lesson.
If the input is a word, its first character is output.
If the input is a sentence, its first word is output.
If the input is a word, its last character is output.
If the input is a sentence, its last word is output.
Since we will be working with an input that is a sentence composed of the row and column numbers, we need a way to access each of them individually. The first operator will be used to get the row number and the last operator will be used to get the column number.
gridGoToRowColCellHere is gridGotoRowColCell, the row and column (RowCol) input derivative of gridGotoCell.
gridGoToRowColCellCtr and gridRowColCellFill
I can't repeat enough how important symmetry is; it is a key aid in remembering groups of things. There are two other procedures which take an index as an input. We should provide RowCol counterparts for gridGotoCellCtr and gridCellFill.
Removing Redundant Code
By now, you should know how I feel about multiple copies of identical code in a program!
Well, take a look at Figure 10.7, a comparison of gridGotoCellCtr and gridGotoRowColCellCtr.
|to gridGotoCellCtr :idx||to gridGotoRowColCellCtr :rowCol|
|gridGotoCell :idx||gridGotoRowColCell :rowCol|
|setheading 90 forward gridHafCellSiz||setheading 90 forward gridHafCellSiz|
|right 90 forward gridHafCellSiz||right 90 forward gridHafCellSiz|
What is very interesting about these two procedures it that the RowCol version is almost identical to the index version. When I first wrote gridGotoRowColCellCtr and gridRowColCellFill, I just copied their index counterparts, changed their names, and changed the first line in their bodies. And, YES, as soon as I saw what I ended up with, I decided to remove the redundant code.
First, I created a couple of empty helper procedures.
Then, I copied & pasted duplicate code into them.
Finally, I replaced the copied code with invocations to the helper procedures. Here is all the code - the results.
Hopefully you will agree that this code is easier to read/understand and have a bit more confidence that it does what we want. And, once again, the less code that makes up our Grid Toolkit, the less chance we have mad a mistake in it.
Maintaining the Grid Toolkit's Contract
Note: It appears as if I have broken the the Grid Toolkit contract. Remember the rule:
All procedures in the toolkit will have names starting with "grid"
Well, what I meant the rule to be is:
All procedures in the toolkit will have names starting with "grid," except for procedures only intended to be invoked inside the toolkit
The helper procedures I added are only for use by other Grid Toolkit code. As an example, fillCellHelper is only to be used by gridCellFill and gridRowColCellFill. And the same limitation applies to gotoCellCtrHelper.
Drawing Text on the Graphics Canvas
Ok, one last thing to complete our lesson on text and its manipulation. All of our use of it so far has been directed to its use as data and to its display in the CommandCenter. How about displaying characters, words, and sentences in the graphics canvas.
The turtle knows how to write. The three primitive commands you use to tell the turtle to draw some characters are: label, setlabelfont, and setlabelheight. The label command draws text on the graphics canvas. The text is drawn with it's lower-left corner where the turtle is. The setlabelfont command determines the face of the characters that are painted. The setlabelheight command controls the size of the characters.
The text representing LABEL's input is drawn on the graphics canvas.
Sets the font face and style of characters drawn with the LABEL procedure.
The height of capital letters drawn with the LABEL procedure is set to the input.
Go back up to the TG applet and play with the label, setlabelfont, and setlabelheight commands a bit...
Revisiting Characters As Numbers
Ok, you now know about words; words can be included in your programs simply by prefixing them with a double quote character ("). Words can be built using the word operator. You now know about sentences; they can be included in your programs by enclosing the words that the sentence consists of within square brackets ([ and ]). There is also a sentence operator.
Words are composed of characters, a single character word can either be thought of as a word or a character. In the first lesson, I introduced you to the concept: "stuff in a computer is just bunches of bits, really just numbers." In the "Symbols as Bits - ASCII Characters" section of that lesson, I introduced the ASCII standard as a ubiquitous way of mapping numbers to characters. So, a final thing to cover in this lesson is how Logo helps you convert between numbers and characters.
|ASCII||character||Outputs the integer (between 0 and 127) that represents the character in the ASCII standard code.|
|CHAR||number||outputs a single character word that is the ASCII code corresponding to the input (an integer between 1 and 127).|
So, given any number in the integer range 1..127, the ascii operator will get you the character the number represents. Or, vice versa, given a single character word, the char operator outputs its numeric equivilent. Figure 10.8 shows some example usages.
Checkout the last two instructions in Figure 10.8 closely. Notice how numbers are considered multiple character words in one case but with the help of char can be reduced to a single character.
Go back up to the TG applet and play with the ascii and char operators a bit to get a better feel for what they can do and what they can't do.
Practice: Random Words in Random Spots
Ok, here's a little exercise that will get you some experience using some of the procedures introduced in this lesson.
The challenge for you is to write a program that draws nonsense words made up of two random letters, the first an uppercase character and the second a lowercase character. Use label to draw the word on the graphics canvas. Figure 10.9 shows what the graphics canvas looked like after my version of the program drew 26 random two-letter words.
Make sure you use procedures introduced in this lesson where you can, e.g., use setpos to move the turtle instead of setxy.
Hint: the randomInRange procedure we wrote in the previous lesson helps out a lot!
One More Grid Toolkit Extension
And, lastly, now that we have a way of drawing characters on the graphics canvas, let's add a couple new commands to our Grid Toolkit.
I'll give you the helper procedure and it is your job to write the procedures that use it. Here's the code; read through it... Do you understand the code?
Well... well... well... There's a new jLogo primitive operator in the code that you haven't seen yet. LABELWIDTH outputs how wide whatever its input will be if it is drawn onto the graphics canvas with the LABEL command. I used it to center the character horizontally within a grid cell.
Ok, it's your turn; write two new procedures:
Practice: Simple Grid Extension
If you have the TG programming environment on your computer, follow this link to the source code for our new GridToolkit and copy & paste the source code into TG's Editor so that you can play with it. If you want to use the TG applet on this web page to play, use the "loadcode GridToolkit_10.jlogo" directive in the CommandCenter to get the source code in the Editor. Make sure the Editor is open before you type in the loadcode directive.Then extend it to put random characters in the cells with gridRowColCellLabel. Figure 10.10 shows a grid my program generated. I also made the labels (characters) random colors.
In this lesson, you learned about symbols (words) and combining them to form sentences. You learned how and why you might want to include sentences in your programs - as a new kind of compound data type. And, finally, you also learned how to draw text onto the graphics canvas.
Go to the Table of Contents
On to User Interface Events