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 four things happens:

  1. a space is encountered,
  2. the end of the sentence the word is part of is encountered,
  3. a semicolon is encountered (the start of a comment), or
  4. 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'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 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 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 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 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.

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

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.

   println word "Introduction word "To word "Computer "Programming   

Figure 10.6 shows a plumbing diagram of this instruction.

Figure 10.6

Type it into the TG applet above 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.

   to showAddTwoNumbers :num1 :num2
     println sentence :num1 sentence "+ sentence :num2 sentence "= (sum :num1 :num2)   
     end

Draw a plumbing diagram of the println instruction, the body of the procedure.  Open up the Editor in the TG applet above 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 also accepts a variable number of inputs to a word or sentence operator.  You do this by surrounding the operator name and all of the inputs to it within parenthesis.  This saves typing and makes understanding what an instruction is doing easier.  Here is what the examples above look like when the variable number of inputs syntax is employed.

   println (word "Introduction "To "Computer "Programming)
			
   to showAddTwoNumbers :num1 :num2
     println (sentence :num1 "+ :num2 "= (sum :num1 :num2))   
     end

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

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

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

   ;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.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
   end    end
Figure 10.7

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

Command Inputs Description
 LABEL  wordOrSentence 

The text representing LABEL's input is drawn on the graphics canvas.

 SETLABELFONT 
 SETLF
 number

Sets the font face and style of characters drawn with the LABEL procedure.

  Number  
 Font 
 Style 
0
Courier
Plain
1
Courier
Bold
2
Courier
Italic
3
Courier
Bold Italic
4
Sans Serif
Plain
5
Sans Serif
Bold
6
Sans Serif
Italic
7
Sans Serif
Bold Italic
8
Serif
Plain
9
Serif
Bold
10
Serif
Italic
11
Serif
Bold Italic
 SETLABELHEIGHT 
 SETLH
 number

The height of capital letters drawn with the LABEL procedure is set to the input.

Table 10.5

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.

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

Figure 10.8

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.

Figure 10.9

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?

   ;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
     setlabelheight difference gridCellSize 8
     forward difference gridCellSize 4
     left 90
     forward quotient (difference gridCellSize (labelwidth :ch)) 2
     setpencolor :color
     label :ch
     end 

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:

   ;label the specified cell with a character   
   to gridCellLabel :idx :ch :color
     ...
     end

   ;label the specified cell with a character
   to gridRowColCellLabel :rowCol :ch :color
     ...
     end

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.

Figure 10.10

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 draw text onto the graphics canvas.


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

Public Domain Mark
This work (BFOIT: Introduction to Computer Programming, by Guy M. Haas),
identified by Berkeley Foundation for Opportunities in IT (BFOIT),
is free of known copyright restrictions.