BFOIT - Introduction to Computer Programming

Words and Sentences

Introduction

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, 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 three things happens:

  1. a space is encountered,
  2. the end of the sentence the word is part of is encountered, or
  3. the end of the line is encountered.

"Word is a word made up of the characters: W, o, r, and d.  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.

Figure 10.1

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. "Logo is a quoted word), a sentence can be included in a program by surrounding a word or words with square brackets.  Here are a few example sentences:

   [This is a sentence]
   [200 50]
   [forward 100 right 90]    

Note that Logo sentences are just lists of words and that they do not have grammatic structure.

The second example shows how we might use 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 what happens when you attempt to invoke println with multiple words.

Figure 10.2

So, even though the sentence [This is a sentence] is composed of four words, println displays it properly.  println expected one thing 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 above, [200 50], is useful as a way of representing a position in TurtleSpace, a point.  Table 10.1 introduces the pos operator. 

Operator Description
 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.
Table 10.1

Figure 10.3 demonstrates that pos outputs a sentence, two words, two numbers in this case.

Figure 10.3

WORD and SENTENCE Operators

Words and sentences can also be assembled using a couple of primitive Logo operators that are available.

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 two words; a sentence can be built by combining words and/or sentences.

Table 10.2 introduces the new word and sentence operators we'll work with for the rest of this lesson.

Operator Inputs Description
 SE
 SENTENCE  
 wordOrSentence1 
 wordOrSentence2 
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.
Table 10.2

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.

Figure 10.4

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.  Figure 10.5 shows what will happen if you try.

Figure 10.5

Oh, and just an fyi... sentence can be abbreviated se.

Here is the TG applet.  Spend a little time playing with the word and sentence operators.

alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason." Your browser is completely ignoring the <APPLET> tag! TG - TurtleGraphics Applet

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).

Command Inputs Description
 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.
Table 10.3

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.

  repeat 50 [setpos se difference (random 500) 250 random 100 wait 500]  

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 that are available.  I need to briefly cover two operators which you can use to access pieces of sentences.

Logo has many operators for manipulating words and sentences, but we are going to initially limit ourselves to working with only 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.

Operator Inputs Description
 FIRST  wordOrSentence If the input is a word, its first character is output.
If the input is a sentence, its first word is output.
 LAST  wordOrSentence If the input is a word, its last character is output.
If the input is a sentence, its last word is output.
Table 10.4

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.

gridGoToRowColCell

Here is gridGotoRowColCell, the row and column (RowCol) input derivative of gridGotoCell.

   ;move turtle to top-left corner of specified cell   
   to gridGotoRowColCell :rowCol
     gridGotoTopLeft
     setheading 180
     forward product gridCellSize (first :rowCol)
     setheading 90
     forward product gridCellSize (last :rowCol)
     end 

gridGoToRowColCellCtr and gridRowColCellFill

There are two other procedures which take an index as an input.  We also need RowCol counterparts for gridGotoCellCtr and gridCellFill

   ;move turtle to the center of specified cell
   to gridGotoRowColCellCtr :rowCol
     gridGotoRowColCell :rowCol
     setheading 90 forward gridHafCellSiz
     right 90 forward gridHafCellSiz
     end

   ;draw/redraw one cell of the grid
   to gridRowColCellFill :rowCol :color
     gridGotoRowColCellCtr :rowCol
     setpensize gridCellSize setpencolor :color
     setheading 0 pendown
     forward gridHafCellSiz back gridCellSize forward gridHafCellSiz  
     penup forward gridHafCellSiz left 90 forward gridHafCellSiz
     setpensize 1 setpencolor gridFrameColor
     setheading 90 pendown
     repeat 4 [forward gridCellSize right 90]
     end 

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.6, 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
   end    end
Figure 10.6

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. 

   ;with the turtle at the top-left corner of
   ;a cell, move it to the center of the cell
   to gotoCellCtrHelper

     end

   ;with the turtle at the top-left corner of a cell,   
   ;repaint the cell with a provided background color
   to fillCellHelper :color

     end 

Then, I copied & pasted duplicate code into them.

   ;with the turtle at the top-left corner of
   ;a cell, move it to the center of the cell
   to gotoCellCtrHelper
     setheading 90 forward gridHafCellSiz
     right 90 forward gridHafCellSiz
     end

   ;with the turtle at the top-left corner of a cell,
   ;repaint the cell with a provided background color
   to fillCellHelper :color
     setpensize gridCellSize setpencolor :color
     setheading 0 pendown
     forward gridHafCellSiz back gridCellSize forward gridHafCellSiz   
     penup forward gridHafCellSiz left 90 forward gridHafCellSiz
     setpensize 1 setpencolor gridFrameColor
     setheading 90 pendown
     repeat 4 [forward gridCellSize right 90]
     end 

Finally, I replaced the copied code with invocations to the helper procedures.  Here is all the code - the results.

   ;with the turtle at the top-left corner of
   ;a cell, move it to the center of the cell
   to gotoCellCtrHelper
     setheading 90 forward gridHafCellSiz
     right 90 forward gridHafCellSiz
     end

   ;move turtle to the center of specified cell
   to gridGotoCellCtr :idx
     gridGotoCell :idx
     gotoCellCtrHelper
     end

   ;move turtle to the center of specified cell
   to gridGotoRowColCellCtr :rowCol
     gridGotoRowColCell :rowCol
     gotoCellCtrHelper
     end

   ;with the turtle at the top-left corner of a cell,
   ;repaint the cell with a provided background color
   to fillCellHelper :color
     setpensize gridCellSize setpencolor :color
     setheading 0 pendown
     forward gridHafCellSiz back gridCellSize forward gridHafCellSiz   
     penup forward gridHafCellSiz left 90 forward gridHafCellSiz
     setpensize 1 setpencolor gridFrameColor
     setheading 90 pendown
     repeat 4 [forward gridCellSize right 90]
     end

   ;draw/redraw one cell of the grid
   to gridCellFill :idx :color
     gridGotoCellCtr :idx
     fillCellHelper :color
     end

   ;draw/redraw one cell of the grid
   to gridRowColCellFill :rowCol :color
     gridGotoRowColCellCtr :rowCol
     fillCellHelper :color
     end 

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 GraphicsCanvas.

The turtle knows how to write.  The two primitive commands you use to tell the turtle to draw some characters are: label and setlabelheight.  The label command draws text on the GraphicsCanvas.  The text is drawn where the turtle is.  The setlabelheight command controls how big the characters are. 

Command Inputs Description
 LABEL  wordOrSentence  The text representing LABEL's input is drawn on the GraphicsCanvas.
 SETLABELHEIGHT 
 SETLH
 number The height of characters drawn with the LABEL procedure is set to the input.

  Number  
 CharHeight 
 CharWidth 
12
9
7
14
10
8
16
12
9
18
14
10
24
18
14
32
22
18
Table 10.5

Go back up to the TG applet and play with the label 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.

Operator Inputs Description
 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).
Table 10.6

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.7 shows some example usages.

Figure 10.7

Checkout the last two instructions in Figure 10.7 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 GraphicsCanvas.  Figure 10.8 shows what the GraphicsCanvas looked like after my version of the program drew 26 random two-letter words.

Figure 10.8

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 GraphicsCanvas, let's add a couple new commands to our Grid Toolkit.  Well, how about if I give you the helper procedure and let you write the procedures gridCellLabel :idx :ch :color and gridRowColCellLabel :rowCol :ch :color.

   ;with the turtle at the top-left corner of a cell, label   
   ;the cell with a provided character in a provided color
   to labelCellHelper :ch :color
     setheading 180
     forward quotient (difference gridCellSize 22) 2
     forward 22
     left 90
     forward quotient (difference gridCellSize 18) 2
     setlabelheight 32
     setpencolor :color
     label :ch
     end 

Practice: Simple Grid Extension

OK... take your new GridToolkit and paste it into an empty TG Editor and then extend it to put random characters in the cells with gridRowColCellLabel.  Figure 10.9 shows a grid my program generated.  I also made the labels (characters) random colors.

Figure 10.9

Summary

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 add text to TurtleSpace via the label command.


Back to Defining Operators
Go to the Table of Contents
On to User Interface Events