The BFOIT Introduction to Programming Class
Appendix B
(TurtleTalk Primitive (Built-in) Procedures)


TurtleTalk's primitive procedures are core building blocks of the language.

*NOTE* You can not use them for names of things you create, like your own procedures.



TurtleTalk Data Manipulation Procedures
Name Input(s) Description Example
BUTFIRST
BF
wordOrSentence If the input is a word, all characters except its first are output. If the input is a sentence, all words except the first are output. BUTFIRST :WD
BUTLAST
BL
wordOrSentence If the input is a word, all characters except its last are output. If the input is a sentence, all words except the last one are output. BUTLAST :SENT
CHAR number outputs a single character word that is the ASCII code corresponding to the input (an integer between 0 and 127). CHAR 65
COUNT wordOrSentence If the input is a word, the number of characters in it is output. If the input is a sentence, the number of words in it is output. COUNT :WD
EMPTYP
EMPTY?
wordOrSentence Outputs true if its input is a word that has no characters in it, otherwise false. Outputs true if its input is a sentence that has no words in it, otherwise false. EMPTY? :SENT
FIRST wordOrSentence If the input is a word, its first character is output. If the input is a sentence, its first word is output. FIRST :WD
ITEM number
wordOrSentence
Outputs the number-th element of its second input. If its second input is a word, it outputs a word consisting on the number-th character. If its second input is a sentence, it outputs the number-th word of it. ITEM 2 :SENT
LAST wordOrSentence If the input is a word, its last character is output. If the input is a sentence, its last word is output. LAST :WD
MEMBERP
MEMBER?
characterOrWord
wordOrSentence
Outputs true if its first input is in its second input, otherwise it outputs false. MEMBER? "e :WD
SE
SENTENCE
wordOrSentence
wordOrSentence
Outputs a sentence consisting of its first input concatenated with its second input. SENTENCE "word :sent
WORD word1
word2
Outputs a word consisting of its first input concatenated with its second input. WORD "$ :word


TurtleTalk Flow-Control Procedures
Name Input(s) Description Example
IF trueFalse
instructionList
If the trueFalse input is true, the instructionList input is performed. If trueFalse is false, nothing is done. IF LESS? MOUSEX 0 [ ... ]
OUTPUT value The current invocation of the procedure in which OUTPUT exists terminates and the value it outputs is value. OUTPUT RANDOM 16
REPEAT count
instructionList
The instructionList input is performed the number of times specified by the count input. REPEAT 4 [ FD 100 RT 90 ]
STOP   The current invocation of the procedure in which STOP exists terminates. IF EQUAL? :inp 0 [STOP]
WAIT number Performing instructions is suspended for the number of milliseconds (1/1000s of a second). WAIT 1000


TurtleTalk Graphics Procedures
Name Input(s) Description Example
BACK
BK
number Moves the turtle backward, i.e., exactly opposite to the direction that it's facing, by the specified number of turtle steps. BACK 150
CG
CLEAN
  Erases everything that the turtle has drawn on the graphics window.  The turtle's state (position, heading, pen color, etc...) is not changed.  
COLORUNDER   The color under the turtle, at the tip of the pen, is output. PRINTLN COLORUNDER
FILL   Whatever color is under the turtle is changed to the turtle's color, and this process is repeated in an outward manner for all pixels that are neighbors and match the original color. FILL
FORWARD
FD
number Moves the turtle forward, in the direction it is facing, by the specified number of turtle steps. FD 100
HEADING   Outputs the turtle's heading in degrees.  
HIDETURTLE
HT
  Hides the turtle, makes it invisible.  
HOME   Moves the turtle to the center of the graphics window, i.e., coordinates 0,0.  
LABEL wordOrSentence The text representing LABEL's input is drawn in the graphics window. LABEL "START
LEFT
LT
number Turns the turtle counterclockwise by the specified angle measured by a number of degrees (1/360 of a circle). LEFT 180
NOREFRESH   Turns off collection of all of the graphics operations. This is useful when writing animation-oriented applications. NOREFRESH
PENDOWN
PD
  Puts the turtle's pen down so that it leaves a trace when it moves.  
PENUP
PU
  Lifts the turtle's pen up so that it leaves no trace when it moves.  
POS   Outputs the current coordinates of the turtle as a sentence. The X coordinate is the FIRST of the sentence; the Y coordinate is the LAST of the sentence. MAKE "CURLOC POS
REFRESH   Turns on collection of all of the graphics operations. Only needed to redraw stuff when the TG application window is resized. REFRESH
RIGHT
RT
number Turns the turtle clockwise by the specified angle, measured in degrees (1/360 of a circle). RT 90
SETHEADING
SETH
number Turns the turtle to a new absolute heading. The input, number, is the heading (in degrees) clockwise from the positive Y axis, i.e.,
0 is due North (up),
90 is due East (right),
180 is due South (down), and
270 is due West (left).
SETH 90
SETLABELHEIGHT
SETLH
number The height of characters drawn with the LABEL procedure is set to the input. SETLABELHEIGHT 32
SETPENCOLOR
SETPC
number Sets the turtle's color - the color of its pen. Color is expressed as a number.

Number
Color
Number
Color
Number
Color
Number
Color
0
black
4
red
8
brown
12
salmon
1
blue
5
magenta
9
tan
13
violet
2
green
6
yellow
10
forest
14
orange
3
cyan
7
white
11
aqua
15
grey
SETPC 10
SETPENSIZE
SETPS
number Sets the width of the turtle's pen, which determines the thickness of the trace it leaves, the line it draws. SETPS 5
SETPOS sentence Moves the turtle to an absolute position, specified by a sentence containing two numbers. The two numbers are (FIRST) a new horizontal (X) coordinate and (LAST) a new vertical (Y) coordinate. SETPOS [100 100]
SETX number Moves the turtle horizontally to the specified absolute window position. SETX 100
SETXY number1
number2
Moves the turtle to an absolute position, specified as two numbers: the new horizontal (X) and vertical (Y) coordinates. SETXY 50 -30
SETY number Moves the turtle vertically to the specified absolute window position. SETY 100
SHOWTURTLE
ST
  Makes the turtle visible.  
XCOR   Outputs the turtle's X-coordinate.  
YCOR   Outputs the turtle's Y-coordinate.  


TurtleTalk Logical Procedures
Name Input(s) Description Example
AND trueFalse1  trueFalse2 Outputs true if trueFalse1 AND trueFalse2 are true, false otherwise.

 Input 1   Input 2   Output 
true true true
true false false
false true false
false false false

AND true true
NOT trueFalse Outputs false if trueFalse is true, else true if trueFalse is false - the opposite trueFalse value. NOT true
OR trueFalse1  trueFalse2 Outputs true if trueFalse1 OR trueFalse2 is true, false otherwise.

 Input 1   Input 2   Output 
true true true
true false true
false true true
false false false

OR false false


TurtleTalk Math Procedures
Name Input(s) Description Example
ABS number Outputs the absolute value of number ABS -15
ASCII character Outputs the integer (between 0 and 127) that represents the character in the ASCII code. ASCII "A
CEIL number Outputs the smallest integer (closest to negative infinity) that is not less than number. CEIL 44.223
COS number Outputs the trigonometric cosine of number which is in degrees COS 45
DIFFERENCE number1  number2 Outputs the result of subtracting number2 from number1 DIFFERENCE 15 7
FLOOR number Outputs the largest integer (closest to positive infinity) that is not greater than number. FLOOR 12.875
MINUS number Outputs the negative of number MINUS 122
PRODUCT number1  number2 Outputs the result of multiplying number1 by number2 PRODUCT 10 6
QUOTIENT number1  number2 Outputs the result of dividing number1 by number2 QUOTIENT 12 4
RANDOM number Outputs some integer greater-than or equal-to zero AND less-than number RANDOM 100
REMAINDER number1  number2 Outputs the remainder left after dividing number1 by number2 REMAINDER 17 3
ROUND number Outputs the closest integer to number ROUND 22.45
SIN number Outputs the trigonometric sine of number which is in degrees SIN 45
SQRT number Outputs the square root of number SQRT 16
SUM number1  number2 Outputs the result of adding number1 and number2 SUM 3 4
TAN number Outputs the trigonometric tangent of number which is in degrees  


TurtleTalk Predicates
Name Input(s) Description Example
EQUAL?
EQUALP
value1
value2
If value1 is equal to value2, EQUAL? outputs true. If not, false is output. EQUAL? MOUSEX 0
GREATER?
GREATERP
value1
value2
If value1 is greater than value2, EQUAL? outputs true. If not, false is output. GREATER? MOUSEX 0
LESS?
LESSP
value1
value2
If value1 is less than value2, EQUAL? outputs true. If not, false is output. LESS? MOUSEX 0


Program Management Stuff
Name Input(s) Description Example
[Ctrl]-T   While in the window used to interact with the TurtleTalk interpreter, holding down the [Ctrl] key and pressing the "T" key will print the name of the current turtle and the state of all turtles.  
[Ctrl]-Q   While in the window used to interact with the TurtleTalk interpreter, holding down the [Ctrl] key and pressing the "Q" key will stop whatever the current turtle is doing - a QUIT directive.  
CT
CLEARTEXT
  Erases all of the text in the window used to interact with the TurtleTalk interpreter. CLEARTEXT
PRINTPROCS
PP
  Displays the names and inputs of all procedures that have been defined in the current session in the terminal window. PRINTPROCS
PRINTTEXT
PT
name Displays the source code representation of the procedure named name in the terminal window. PRINTTEXT main
TRACE procName
varName
Directs TG to display information every time a specified user-defined procedure is invoked or a specified global variable's value is changed. TRACE keyPressed
TRACE curNum
UNTRACE name Cancels the TRACE request for the named procedure or global variable. UNTRACE keyPressed
UNTRACE curNum


TurtleTalk User-Interface Procedures
Name Input(s) Description Example
CANVASHEIGHT   Outputs the current height of the graphics window in turtle steps.  
CANVASWIDTH   Outputs the current width of the graphics window in turtle steps.  
KEYPRESSED keyNumber When a keyboard key is pressed, TG receives an event. If the key is one that TG is interested in, a user- defined procedure with the name KEYPRESSED (expecting one input) is invoked if it has been defined.

 Key Number   Keyboard Key 
10 Enter
32 Space
48 - 57 0 - 9
65 - 90 A - Z
97 - 122 a - z
128 Down-Arrow
129 Left-Arrow
130 Right-Arrow
131 Up-Arrow

TO keyPressed :num
  PRINTLN WORD "key= :num
  END
MOUSECLICK   When a mouse-click is received by the TurtleTalk system, it performs a user-defined procedure with the name MOUSECLICK, if one has been defined.  
MOUSEX   Outputs the X-coordinate where the mouse was when it was clicked.  
MOUSEY   Outputs the Y-coordinate where the mouse was when it was clicked.  
 NEWTURTLE  name
instructionList
Creates a new turtle with the specified name and directs future input to it.

If an optional instructionList is supplied, it is given to the new turtle to do and future input remains directed to the current turtle.

NEWTURTLE "t2 [ HT ]
PRINT thing Displays thing in the terminal window. PRINT MOUSEX
PRINTLN thing Displays thing in the terminal window. The line is then finished off by moving the cursor to the start of the next line. PRINTLN HEADING
 TALKTO name
instructionList
Directs future input to the specified turtle or, if the optional instructionList id provided, it is given to the specified turtle and future input remains directed to the current turtle. TALKTO "t2 [ FD 10 ]


TurtleTalk Variables
Name Input(s) Description Example
ARRAY size Outputs an array of size members, each of which is initially an empty sentence. Size must be a positive integer. MAKE "ARY ARRAY 5
GLOBAL name Declares a global variable named name.  The variable can be referenced anywhere in the program's text following this command.  The variable has no initial contents. GLOBAL "var
LOCAL name Declares a local variable named name.  The variable is local to the procedure it is in.  The variable has no initial contents. LOCAL "var
MAKE name
value
Creates a variable named name if it doesn't already exist.  The contents of the variable is set to value. MAKE "WHITE 7
THING name Outputs the value in its input, a global variable named name.  ":NAME" is an abbreviation for THING "NAME.  But, its real need is when the names of global variables are constructed dynamically. THING :curSequence
SETITEM index
array
value
Replaces the indexth member of array with value. SETITEM 2 :nums "two


Back to Table of Contents