Logo Interpreter

By Joshua Bell | Examples | Links | To Do | Change Log

Your browser does not support the canvas element - sorry!

Examples

to star repeat 5 [ fd 100 rt 144 ] end
star
to square :length repeat 4 [ fd :length rt 90 ] end
repeat 36 [ square 50 rt 10 ]
to randomcolor setcolor pick [ "red "orange "yellow "green "blue "violet ] end
repeat 36 [ randomcolor square random 200 rt 10 ]

To Do

Change Log

2008-09-02
Added reverse, iseq
print can now print lists
Fixed evaluation of lists (e.g. map "square [ 50 + 50 ] now works)
2008-09-01
Rearranged page to maximize display area
2008-08-31
Added map, filter, reduce
Added pencolor as alias for setcolor, can take r, g, b as arguments
Blocks in repeat, if, etc. no longer introduce a scope
Comments (anything after ; on a line) are ignored)
Renamed predicates to equal?, notequal?, less?, lessequal?, greater?, greaterequal?
Changed the "draw text" procedure to print
Added output which returns a value from a procedure
2008-08-30
Lisp-style calling syntax ( procedure input ... ) now operates per standard Logo, rather than as an implicit reduction.
Make use of JavaScript 1.6 and 1.8 functions internally, via compatibility shims
2008-08-26
Fixed tan
Trig functions now take/return degrees
KNOWN ISSUE: setxy -100 -100 parses as setxy (-100 -100) and so fails with too few arguments.
make :var and make var are now synonyms for make "var
Added true and false
2008-08-25
Fixed procedures that return values
Made setpos and friends leave a trail if the pen is down
jslint cleanup
Support ( proc input ... ) format
Fixed and and or to not lazily-evaluate arguments - and 0 1 would not consume the 1
Added support for ( procedure input ... ) expressions. Acts as reduce, so ( sum 1 2 3 4 ) calls sum 1 2 then sum 3 3 then sum 6 4, finally returning 10.
2008-08-24
Added string/number coercion
Added word and output
Allow unquoted variable names in make
Added and, or, xor and setfont
Changed towards to return a heading, rather than set a heading
Added infix expressions
Added first, last, butfirst, butlast
Added power, sqrt, arctan, sin, cos, tan, int, round
Renamed add to sum, sub to difference, mul to product, div to quotient, mod to modulus, neg to minus
2008-08-21
Internal refactoring to simplify parsing
Added pos and heading
2008-08-20
Added stop and towards
Fixed parsing of setpos inputs
Inverted Y axis for absolute coordinate references (positive Y is now upwards)
2008-08-19
Can't use lt for both "left turn" and "less than". The latter is now lth
Added make, setxy, setx, sety, setheading. Updated setpos
Fixed negative number parsing
Added toggle to multiline display
Corrected repcount behavior for nested repeats
2008-08-18
Added mathematical and logical operations
2008-08-17
Added if, ifelse, repcount, clean, home, showturtle, hideturtle
Added procedure inputs
Added display of the turtle
Had left and right switched. Oops.
2008-08-16
Written and published

Language Reference

hide

Types

[ item ... ]
List
"word
Text string (terminated by whitespace)
:variable
Input definition/variable reference
( expression )
Parenthesis can be used to group expressions
( procedure input ... )
Call procedure with an arbitrary number of inputs

Procedures and Flow Control

to procname inputs ... statements ... end
Define a new named procedure with optional inputs
output expr
End the running procedure and output the specified value
stop
Terminate the program
if expr [ statements ... ]
Execute statements if the expression is non-zero
ifelse expr [ statements ... ] [ statements ... ]
Execute first set of statements if the expression is non-zero, otherwise execute the second set
repeat expr [ statements ... ]
Repeat statements expr times
repcount
Return the current iteration number of the current repeat

Variables

make "varname expr
Update a variable or define a new global variable. Quoting the variable name is optional, but this may be unsupported in the future since it precludes making the name itself an expression

String Operations

word expr expr
(word expr ...)
Concatenate two (or more) words into one word, separated with spaces

List Operations

first [ items ... ]
last [ items ... ]
Return first/last item from the list
butfirst [ items ... ]
butlast [ items ... ]
Return list, except for the first/last item
reverse [ items ... ]
Returns a list with the items in reverse order
pick [ items ... ]
Return one item from a list, at random
iseq expr expr
Returns a list with integers from the first to the last, inclusive

Arithmetic Operations

sum expr expr
(sum expr ...)
expr + expr
difference expr expr
expr - expr
product expr expr
(product expr ...)
expr * expr
quotient expr expr
(quotient expr)
expr / expr
modulus expr expr
expr % expr
power expr expr
expr ^ expr
minus expr
- expr
Add, subtract, multiply, divide, modulo (remainder-after-division), negate, and raise-to-the-power-of respectively. Inputs are numbers or numeric expressions, output is a number. A single input to quotient returns the reciprocal.
sqrt expr
arctan expr
(arctan expr expr)
sin expr
cos expr
tan expr
Square root, and the usual trig functions. Inputs are numbers or numeric expressions, output is a number. Angles are in degrees.
random expr
Return a random number from 0 through one less than expr

Logical Operations

and expr expr
(and expr ...)
or expr expr
(or expr ...)
xor expr expr
(xor expr ...)
not expr
Logical "and", "or", "exclusive-or", and "not", respectively. Inputs are numbers or numeric expressions, output is 1 (true) or 0 (false).

Predicates

equalp expr expr
equal? expr expr
expr = expr
notequalp expr expr
notequal? expr expr
expr <> expr
greaterp expr expr
greater? expr expr
expr > expr
greaterequalp expr expr
greaterequal? expr expr
expr >= expr
lessp expr expr
less? expr expr
expr <= expr
lessequalp expr expr
lessequal? expr expr
expr <= expr
Equals, not-equals, greater than, greater than or equal to, less than, less than or equal to, respectively. Inputs are numbers or numeric expressions, output is 1 (true) or 0 (false).

Turtle Graphics

forward expr
fd expr
Move turtle forward expr pixels
backexpr
bk expr
Move backward forward expr pixels
right expr
rt expr
Rotate expr degrees clockwise
left expr
lt expr
Rotate expr degrees counterclockwise
print expr
Write a word (Firefox 3 only)
penup
pu
Turtle stops leaving a trail
pendown
pd
Turtle resumes leaving a trail
clean
Clear the drawing area
home
Return turtle to center, pointing upwards
clearscreen
cs
Same as clean and home together
setpos [ expr expr ]
setxy expr expr
setx expr
sety expr
Move turtle to the specified location without drawing
setheading expr
seth expr
Rotate the turtle to the specified heading
pos
xcor
ycor
Returns the current turtle position as [ x y ], x or y respectively
towards [ expr expr ]
Returns the heading towards the specified [ x y ] coordinates
heading
Returns the current turtle heading
setcolor csscolor
pencolor csscolor
(setcolor expr expr expr)
(pencolor expr expr expr)
Set pen/text color (CSS color names or #rrggbb), must be a string e.g. "red or in the 3-argument version, r/g/b values in 0...255
font cssfont
Set text font (per the CSS 'font' property), must be a string e.g. word "20pt "Verdana
setwidth expr
Set pen width to expr pixels
hideturtle
ht
Hide the turtle
showturtle
st
Show the turtle

Higher Order Functions

map "procname [ expr ... ]
Return a list composed of the results of calling procname for each item in the list
filter "procname [ expr ... ]
Return a list composed of the input list where procname called on the item returns true
reduce "procname [ expr ... ]
(reduce "procname [ expr ... ] initial)
Call procname repeatedly with two arguments - the current value and the next list item. If initial is not specified, the first list element is used instead.