Copyright (C) 2002 by Scott Noveck
This documentation is based on that of Berkely Logo, Copyright (C) 1993
by the Regents of the University of California
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
675 Mass Ave, Cambridge, MA 02139, USA.
This implementation of Logo focuses on the graphical and turtle movement
aspects of the Logo language. It does not attempt to implement the
LISP-derived list processing features, text manipulation, or other advanced
data types found in Berkeley Logo and other fully-featured distributions.
Users familiar with other dialects of Logo should pay particular attention
to this section. Some of the tokenization rules have been modified
slightly in this implementation.
This implementation of Logo ignores whitespace and text formatting. You
may insert spaces and end lines anywhere so long as they do not split apart
whole words/tokens.
All commands, including both built-in tokens and user-specified procedure and variable names, are case-insensitive. Case of letters is preserved in everything you type, however.
Variable names, which include any strings begining with the colon (:)
character, are delimited by a space, a bracket, a parenthesis, a comment
character (see below), or an infix operator (these include +-*/%^=<>,
along with the calculator-specific negative, not-equal, less-than-or-equal,
and greater-than-or-equal characters). Note that the colon character is
not a delimiter.
All other words, including procedure names and built-in tokens, are delimited
by a space, a square bracket, a parenthesis, or a comment character (see
below). Note that this means procedure names may contain infix operators,
such as one-two, but variable names may not.
A semicolon or calculator-specific comment character (diamond-)
on the 89, 2nd-x on the 92+/V200) begins a comment in an instruction
line. Logo ignores characters from the beginning of the comment to the
end of the line.
This Logo implementation differs slightly from past implementations in several aspects. Some of the changes are addressed here:
product procedure
can take only two inputs, or it can take more. In most other Logo implementaions,
if a procedure is passed a number of inputs other than the default, it must
be surrounded by parentheses, as in (product 2 3 4); this is
sign of Logo's parent language, LISP. This implementation works slightly
differently - procedures like product will continue treating
the tokens that follow them as inputs until it finds one that would not
be a valid input. This is done for convenience, as it eliminates the
need for extra parentheses in many instances. However, in some instances
this may cause problems, such as difference product 2 3 4. In
this example, extra parentheses should be added to clarify the desired operation,
resulting in difference (product 2 3) 4. When using any
procedure that can take a variable number of inputs, you should keep in mind
that parentheses may be necessary.
SUM num1 num2
SUM num1 num2 num3 ...
num1 + num2
outputs the sum of its inputs.
DIFFERENCE num1 num2
num1 - num2
outputs the difference of its inputs.
MINUS number
- number
- number (where - is the calculator-specific negative sign)
outputs the negative of its input. Minus sign means unary minus if it is immediately preceded by something requiring an input. There is a difference in binding strength between the two forms:
MINUS 3 + 4 means -(3+4)
- 3 + 4 means (-3)+4
Also note that there is a difference in binding strength from the calculator's
built-in mathematical functions. The negative sign binds tighter than
the expontiation (power) operation, so -3^2 is the same as
(-3)^2 or 9, not -(3^2) or 9.
This order of operations is more common than the calculator's default.
Note that in many situations, using the minus sign for negation may be
problematic. For instance, because Logo is not sensitive to whitespace,
setxy 10 -10 will be read as setxy 10-10, or setxy
0, and then look at the following text for a second input. Instead,
use the calculator-specific negative sign or parentheses, such as in setxy
10 (-10).
PRODUCT num1 num2
PRODUCT num1 num2 num3 ...
num1 * num2
outputs the product of its inputs.
QUOTIENT num1 num2
QUOTIENT number
num1 / num2
outputs the quotient of its inputs. The quotient of two integers is an integer if and only if the dividend is a multiple of the divisor. (In other words, QUOTIENT 5 2 is 2.5, not 2, but QUOTIENT 4 2 is 2, not 2.0 -- it does the right thing.) With a single input, QUOTIENT outputs the reciprocal of the input.
REMAINDER num1 num2
num1 % num2
outputs the remainder on dividing num1 by num2;
both must be integers and the result is an integer with the same sign as
num1.
MODULO num1 num2
outputs the remainder on dividing num1 by num2;
both must be integers and the result is an integer with the same sign as
num2.
INT number
outputs its input with fractional part removed, i.e., an integer with the same sign as the input, whose absolute value is the largest integer less than or equal to the absolute value of the input.
Note: Inside the computer numbers are represented in two different forms, one for integers and one for numbers with fractional parts. However, on most computers the largest number that can be represented in integer format is smaller than the largest integer that can be represented (even with exact precision) in floating-point (fraction) format. The INT operation will always output a number whose value is mathematically an integer, but if its input is very large the output may not be in integer format. In that case, operations like REMAINDER that require an integer input will not accept this number.
See section remainder
ROUND number
outputs the nearest integer to the input.
SQRT number
outputs the square root of the input, which must be nonnegative.
POWER num1 num2
num1 ^ num2
outputs num1 to the num2 power. If num1 is
negative, then num2 must be an integer.
EXP number
outputs e (2.718281828+) to the input power.
LOG10 number
outputs the common logarithm of the input.
LN number
outputs the natural logarithm of the input.
SIN degrees
outputs the sine of its input, which is taken in degrees.
RADSIN radians
outputs the sine of its input, which is taken in radians.
COS degrees
outputs the cosine of its input, which is taken in degrees.
RADCOS radians
outputs the cosine of its input, which is taken in radians.
TAN degrees
outputs the tangent of its input, which is taken in degrees.
RADTAN radians
outputs the tangent of its input, which is taken in radians.
ARCSIN number
outputs the arcsine, in degrees, of its input.
RADARCSIN number
outputs the arcsine, in radians, of its input.
ARCCOS number
outputs the arccosine, in degrees, of its input.
RADARCCOS number
outputs the arccosine, in radians, of its input.
ARCTAN number
ARCTAN x y
outputs the arctangent, in degrees, of its input. With two inputs, outputs the arctangent of y/x, if x is nonzero, or 90 or --90 depending on the sign of y, if x is zero.
RADARCTAN number
RADARCTAN x y
outputs the arctangent, in radians, of its input. With two inputs, outputs the arctangent of y/x, if x is nonzero, or pi/2 or -pi/2 depending on the sign of y, if x is zero.
The expression 4*(RADARCTAN 1) or 2*(RADARCTAN 0 1)can be used to get the value of pi.
EQUALP num1 num2
EQUAL? num1 num2
num1 = num2
outputs TRUE if its first input is equal to its second.
NOTEQUALP num1 num2
NOTQUAL? num1 num2
num1 ? num2 (where ? is the calculator-specific not-equal symbol)
outputs TRUE if its first input is not equal to its second.
LESSP num1 num2
LESS? num1 num2
num1 < num2
outputs TRUE if its first input is strictly less than its second.
GREATERP num1 num2
GREATER? num1 num2
num1 > num2
outputs TRUE if its first input is strictly greater than its second.
LESSEQUALP num1 num2
LESSEQUAL? num1 num2
num1 < num2 (where < is the calculator-specific less-than-or-equal symbol)
outputs TRUE if its first input is less than or equal to its second.
GREATEREQUALP num1 num2outputs TRUE if its first input is greater than or equal to its second.
GREATEREQUAL? num1 num2
num1 > num2 (where > is the calculator-specific greater-than-or-equal symbol)
RANDOM number
outputs a random nonnegative integer less than its input, which must be an integer.
RERANDOM
RERANDOM seed
command. Makes the results of RANDOM reproducible. Ordinarily the sequence of random numbers is different each time Logo is used. If you need the same sequence of pseudo-random numbers repeatedly, e.g. to debug a program, say RERANDOM before the first invocation of RANDOM. If you need more than one repeatable sequence, you can give RERANDOM an integer input; each possible input selects a unique sequence of numbers.
BITAND num1 num2
BITAND num1 num2 num3 ...
outputs the bitwise AND of its inputs, which must be integers.
See section and
BITOR num1 num2
BITOR num1 num2 num3 ...
outputs the bitwise OR of its inputs, which must be integers.
See section or
BITXOR num1 num2
BITXOR num1 num2 num3 ...
outputs the bitwise EXCLUSIVE OR of its inputs, which must be integers.
See section or
BITNOT number
outputs the bitwise NOT of its input, which must be an integer.
See section not
ASHIFT num1 num2
outputs num1 arithmetic-shifted to the left by num2
bits. If num2 is negative, the shift is to the right with sign extension.
The inputs must be integers.
LSHIFT num1 num2
outputs num1 logical-shifted to the left by num2
bits. If num2 is negative, the shift is to the right with zero fill.
The inputs must be integers.
AND bool1 bool2
AND bool1 bool2 bool3 ...
outputs TRUE if all inputs are TRUE, otherwise FALSE. All inputs must be TRUE or FALSE.
OR bool1 bool2
OR bool1 bool2 bool3 ...
outputs TRUE if any input is TRUE, otherwise FALSE. All inputs must be TRUE or FALSE.
NOT bool
outputs TRUE if the input is FALSE, and vice versa.
FORWARD distance
FD distance
moves the turtle forward, in the direction that it's facing, by the specified distance (measured in turtle steps).
BACK distance
BK distance
moves the turtle backward, i.e., exactly opposite to the direction that it's facing, by the specified distance. (The heading of the turtle does not change.)
LEFT degrees
LT degrees
turns the turtle counterclockwise by the specified angle, measured in degrees (1/360 of a circle).
RIGHT degrees
RT degrees
turns the turtle clockwise by the specified angle, measured in degrees (1/360 of a circle).
SETXY xcor ycor
moves the turtle to an absolute screen position. The two arguments are numbers, the X and Y coordinates.
SETX xcor
moves the turtle horizontally from its old position to a new absolute horizontal coordinate. The argument is the new X coordinate.
SETY ycor
moves the turtle vertically from its old position to a new absolute vertical coordinate. The argument is the new Y coordinate.
HOME
moves the turtle to the center of the screen. Equivalent to SETXY 0 0.
SETHEADING degrees
SETH degrees
turns the turtle to a new absolute heading. The argument is a number, the heading in degrees clockwise from the positive Y axis.
XCOR
outputs a number, the turtle's X coordinate.
YCOR
outputs a number, the turtle's Y coordinate.
HEADING
outputs a number, the turtle's heading in degrees.
TOWARDS xpos ypos
outputs a number, the heading at which the turtle should be facing so that it would point from its current position to the position given as the argument.
SHOWTURTLE
ST
makes the turtle visible.
HIDETURTLE
HT
makes the turtle invisible. It's a good idea to do this while you're in the middle of a complicated drawing, because hiding the turtle speeds up the drawing substantially.
CLEAN
erases all lines that the turtle has drawn on the graphics window. The turtle's state (position, heading, pen mode, etc.) is not changed.
CLEARSCREEN
CS
erases the graphics window and sends the turtle to its initial position and heading. Like HOME and CLEAN together.
See section home
SHOWNP
SHOWN?
outputs TRUE if the turtle is shown (visible), FALSE if the turtle is hidden. See SHOWTURTLE and HIDETURTLE.
See section showturtle ; section hideturtle
The turtle carries a pen that can draw pictures. At any time the pen can be UP (in which case moving the turtle does not change what's on the graphics screen) or DOWN (in which case the turtle leaves a trace). If the pen is down, it can operate in one of three modes: PAINT (so that it draws lines when the turtle moves), ERASE (so that it erases any lines that might have been drawn on or through that path earlier), or REVERSE (so that it inverts the status of each point along the turtle's path).
PENDOWN
PD
sets the pen's position to DOWN, without changing its mode.
PENUP
PU
sets the pen's position to UP, without changing its mode.
PENPAINT
PPT
sets the pen's position to DOWN and mode to PAINT.
PENERASE
PE
sets the pen's position to DOWN and mode to ERASE.
See section erase
PENREVERSE
PX
sets the pen's position to DOWN and mode to REVERSE. (This may interact in hardware-dependent ways with use of color.)
See section reverse
PENDOWNP
PENDOWN?
outputs TRUE if the pen is down, FALSE if it's up.
TO procname :input1 :input2 ... (special form)
command. Begins a procedure definition. The procedure will be named
procname and there must not already be a procedure by that
name. The inputs will be called input1 etc. Any number of
inputs are allowed, including none. Names of procedures and inputs are
case-insensitive.
Unlike every other Logo procedure, TO takes as its inputs the names of arguments that will be passed to the procedure.
This version of Logo also allows a variable numbers of inputs to a procedure. After all the required inputs are specified, there can be zero or more optional inputs, represented by the following notation:
[:inputname default.value.expression]
In this version of logo, the "main" procedure - the first procedure in
the file and the one whose name matches the filename - handles inputs specially.
When the program is run, the user will be prompted for the values of
these inputs via a dialog with text entry boxes. These boxes will be
blank for normal inputs, or pre-filled with the default values for inputs
for inputs specified with the optional input syntax. See the example
programs included with this distribution for examples.
Procedures cannot return values; however, the can pass values to to their
caller via global variables such as in this example:
to proc1
proc2
fd :output
end
to proc2
make :output 10
end
MAKE :varname value
command. Assigns the value value to the variable named varname,
which must be a word. Variable names are case-insensitive. If a variable
with the same name already exists, the value of that variable is changed.
If not, a new global variable is created.
Not that the syntax here differs from most other Logo implementations.
For the sake of simplicity, the variable name to store the value to
should be preceded by a colon, as with everywhere else a variable name is
specified.
LOCAL :varname
LOCAL :varname1 :varname2 ...
command. Accepts as inputs one or more words, or a list of words. A
variable is created for each of these words, with that word as its name.
The variables are local to the currently running procedure. Logo variables
follow dynamic scope rules; a variable that is local to a procedure is
available to any subprocedure invoked by that procedure. The variables
created by LOCAL have no initial value; they must be assigned a value (e.g.,
with MAKE) before the procedure attempts to read their value.
Local variables are useful because the memory they use is freed as soon
as a function is completed. Global variables, on the other hand, will
reserve their memory until the program has finished executing.
See section make
PROCEDUREP name
PROCEDURE? name
outputs TRUE if the input is the name of a procedure.
PRIMITIVEP name
PRIMITIVE? name
outputs TRUE if the input is the name of a primitive procedure (one built into Logo). Note that some of the procedures described in this document are library procedures, not primitives.
DEFINEDP name
DEFINED? name
outputs TRUE if the input is the name of a user-defined procedure, including a library procedure. (However, Logo does not know about a library procedure until that procedure has been invoked.)
NAMEP name
NAME? name
outputs TRUE if the input is the name of a variable.
REPEAT num [ instructionlist ]
command. Runs the instructionlist repeatedly, num
times.
REPCOUNT
outputs the repetition count of the innermost current REPEAT, starting from 1. If no REPEAT is active, outputs --1.
IF condition [ instructionlist ]
command. If the first input has the value TRUE, then IF runs the second input. If the first input has the value FALSE, then IF does nothing. It is an error if the first input is not either TRUE or FALSE.
IFELSE condition [ instructionlist1 ] [ instructionlist2 ]
command or operation. If the first input has the value TRUE, then IFELSE runs the second input. If the first input has the value FALSE, then IFELSE runs the third input.
TEST condition
command. Remembers its input, which must be TRUE or FALSE, for use by later IFTRUE or IFFALSE instructions. The effect of TEST is local to the procedure in which it is used; any corresponding IFTRUE or IFFALSE must be in the same procedure or a subprocedure.
See section iffalse
IFTRUE [ instructionlist ]
IFT [ instructionlist ]
command. Runs its input if the most recent TEST instruction had a TRUE input. The TEST must have been in the same procedure or a superprocedure.
IFFALSE [ instructionlist ]
IFF [ instructionlist ]
command. Runs its input if the most recent TEST instruction had a FALSE input. The TEST must have been in the same procedure or a superprocedure.
See section test
STOP
command. Ends the running of the procedure in which it appears. Control is returned to the context in which that procedure was invoked.
PAUSE
command or operation. Pauses until the user presses a key. Note that this function works differently here than in many other implementations of Logo.
WAIT time
command. Delays further execution for time 20ths of a second.
FOR [ :varname start end step ] [ instructionlist ]
command. The first input must be a list containing three or four members: (1) a variable name, which will be used as the name of a local variable; (2) a number, the starting value of the variable; (3) a a number, the limit value of the variable; (4) an optional number that will be step size. If the fourth member is missing, the step size will be 1 or -1 depending on whether the limit value is greater than or less than the starting value, respectively.
The second input is an instructionlist. The effect of FOR is to run that instructionlist repeatedly, assigning a new value to the control variable (the one named by the first member of the forcontrol list) each time. First the starting value is assigned to the control variable. Then the value is compared to the limit value. FOR is complete when the sign of (current-limit) is the same as the sign of the step size. (If no explicit step size is provided, the instructionlist is always run at least once. An explicit step size can lead to a zero-trip FOR, e.g., FOR [I 1 0 1] ...). Otherwise, the instructionlist is run, then the step is added to the current value of the control variable and FOR returns to the comparison step.
DO.WHILE [ instructionlist ] condition
command. Repeatedly evaluates the instructionlist as long
as the evaluated tfexpression remains TRUE. Evaluates the
first input first, so the instructionlist is always run at
least once. The tfexpression must be an expressionlist whose
value when evaluated is TRUE or FALSE.
WHILE condition { instructionlist ]
command. Repeatedly evaluates the instructionlist as long
as the evaluated tfexpression remains TRUE. Evaluates the
first input first, so the instructionlist may never be run
at all. The tfexpression must be an expressionlist whose value
when evaluated is TRUE or FALSE.
DO.UNTIL [ instructionlist ] condition
command. Repeatedly evaluates the instructionlist as long
as the evaluated tfexpression remains FALSE. Evaluates the
first input first, so the instructionlist is always run at
least once. The tfexpression must be an expressionlist whose
value when evaluated is TRUE or FALSE.
UNTIL condition [ instructionlist ]
command. Repeatedly evaluates the instructionlist as long
as the evaluated tfexpression remains FALSE. Evaluates the
first input first, so the instructionlist may never be run
at all. The tfexpression must be an expressionlist whose value
when evaluated is TRUE or FALSE.