Loko

Loko is an interactive interpreter for the Logo programming language. Logo is best known for beginner-oriented “Turtle Graphics” functionality, but internally the language is actually a dynamically-scoped member of the Lisp family. As such, Logo provides sophisticated list manipulation facilities and support for higher-order functions.

All values in Logo are either signed integers, words or lists. Words can contain alphanumeric characters as well as .,?!, and when a word is encountered it is executed. If a word is prefaced with a ', it instead represents the name of the word. If a word is prefaced with a :, it is a variable, and represents the value associated with the given name. Lists are enclosed in square brackets and none of the words within a list are executed immediately.

Logo expressions are given in prefix-notation, with the arguments to the right of the operators. Several operators (+-*/%<>=) also have an optional infix syntax. Infix expressions must always be separated from prefix expressions by parentheses.

Turtle Graphics

At startup, Loko awaits instructions in a scrolling command-line mode. The showturtle command switches to Turtle Graphics mode and hideturtle switches back. Turtle Graphics centers around manipulating the triangular object on the screen (the turtle) by instructing it to move or turn. As the turtle moves around the screen, it draws a line- there are commands for disabling the pen or changing the color it draws with. Instructions can be entered individually or together on a single line:

showturtle
forward 100
right 75 forward 50 right 30 forward 20

Defining Procedures

Procedures are defined by using the to keyword, specifying a series of arguments, and a procedure body, terminated by end. When a line beginning with to is entered at the Loko command prompt, the line editor will expand and allow the procedure to be freely edited with the keyboard and cursor keys. If return is pressed with the last line of this editor selected, the procedure will be entered and further commands can be issued. Previously entered procedures can be modified by using the edit command and specifying a word corresponding to a procedure name.

Here’s an example procedure calculating the maximum of two values:

to maximum :a :b
	if less? :a :b [ output :b ]
	output :a
end

Flow Control and I/O

Logo provides a minimal set of flow control primitives:

Logo also provides primitives for reading from and printing to the command console:

Math

Type Predicates

To distinguish between value types, Logo provides a set of predicates. Just like the comparison operators, these routines return either 'true or 'false as a word.

List Operations

All list operations are functional- that is, they return a new list rather than altering an existing list in-place:

Misc

Examples

Recursively Draw a Spiral:

to spiral :x
	if equal? :x 0 [stop]
	fd :x rt 45
	spiral difference :x 1
end

Novel Control Structures:

to forever :proc
	run :proc
	forever :proc
end

to while :cond :proc
	unless run :cond [ stop ]
	run :proc
	while :cond :proc
end

A Push-Down Stack:

to push :stack :value
	make :stack fput :value thing :stack
end

to pop :stack
	local  'top      first thing :stack
	make   :stack butfirst thing :stack
	output :top
end

Recursive Tree:

to branch :a :b :depth
	fd 10
	tree difference :depth 1
	setheading :a
	setpos     :b
end

to tree :depth
	if equal? :depth 0 [stop]
	local 'dir   heading
	local 'place pos
	lt 15 branch :dir :place :depth
	rt 15 branch :dir :place :depth
end

Dragon Curve:

to x :c
	if (:c = 0) [ stop ]
	x  (:c - 1)
	right 90
	y  (:c - 1)
	forward 4
end

to y :c
	if (:c = 0) [ stop ]
	forward 4
	x  (:c - 1)
	left  90
	y  (:c - 1)
end

to dragon
	showturtle
	x 11
end

Curses:

to any :list
	output item random size :list :list
end

to noun
	output any [
		[an enraged camel]
		[an ancient philosopher]
		[a cocker spaniel]
		[the Eiffel Tower]
		[a cowardly moose]
		[the silent majority]
	]
end

to verb
	output any [
		[get inspiration from]
		[redecorate]
		[become an obsession of]
		[make a salt lick out of]
		[buy an interest in]
	]
end

to object
	output any [
		[mother in law]
		[psychoanalyst]
		[rumpus room]
		[fern]
		[garage]
		[love letters]
	]
end

to curse
	pr	fput 'May
		fput noun
		fput verb
		fput 'your
		list object '!
end