| |||||||||
The Logo programming language is an adaptation by Wally Feurzeig and Seymour Papert of the Lisp programming language that is easier to read. One could say that Logo is Lisp without the parentheses. Today, it is known principally for its "turtle graphics" but it has significant list handling facilities, file handling and I/O facilities and can be used to teach most computer science concepts, as Brian Harvey does in his "Computer Science Logo Style" trilogy. Equally it can be used to prepare "microworlds" for students to investigate.
There are over 130 implementations of Logo, each of which has its own strengths. A popular cross-platform implementation is UCBLogo. MSWLogo, its freeware Windows derivative, is commonly used in schools in the United Kingdom. Comenius Logo is available in Dutch, German, Czech etc. and is worth considering.
A modern derivative of Logo is a variation that allows thousands of "turtles", each moving independently. There are two popular implementations: MIT StarLogo and NetLogo. These derivatives allow for the exploration of emergent phenomena and come with many experiments in social studies, biology, physics, and many other sciences. Although the focus is on the interactions of a large number of independent agents, these variations still capture the original flavor of Logo.
There is no single agreed-upon Logo language definition or standard, only a loose tradition. As a result, there are substantial differences between the many dialects of Logo that have evolved. The code examples shown below would work in many Logo dialects, but not all.
The idea is that a turtle with a pen strapped to it can be instructed to do simple things like move forward 100 spaces or turn around. From these building blocks you can build more complex shapes like squares, triangles, circles--using these to draw houses or sail boats.
The turtle moves with commands that are relative to its own position, "LEFT 90" meant rotate left by 90 degrees. A student could understand (and predict and reason about) the turtle's motion by imagining what they would do if they were the turtle. Papert called this "body syntonic" reasoning.
The idea of turtle graphics is also useful for example in Lindenmayer system for generating fractals.
The following are examples of Turtle code. While seemingly very simple, titles can be given to groups of instructions, essentially creating libraries of more complex commands. In practice short forms are used. For example, "LEFT 90" is written "LT 90".
Key words are usual written in UPPER CASE for beginners, but more advanced texts use lower case.
FORWARD 100
LEFT 90
FORWARD 100
LEFT 90
FORWARD 100
LEFT 90
FORWARD 100
This would draw a square with sides 100 units long ( but the turtle still has to turn LT 90 to be in the starting position).
The commands may be written on one line, or more.
FORWARD 100 RIGHT 120
FORWARD 100 RIGHT 120 FORWARD 100
Would draw a triangle.
The turtle's pen could be lifted and lowered; drawing a dotted line was rudimentary. In this example we'll use the short form for FORWARD, which is FD. (A typical command can then be read "FD space 10", specifying everything clearly and saving frustration.) Anything written after the ; (semicolon) is ignored, allowing the coder to insert comments.
You could also use loop (repeat) commands. This would draw the exact same box as in the first example:
REPEAT 4 [FD 100 RIGHT 90]
Which would execute the command "FD 100 RIGHT 90" four times.
A simplistic circle consists of 360 individual rotations with a step forward, so "REPEAT 360 [FD 1 RIGHT 1]" would have the expected result.
You can teach the turtle new words, i.e. groups of instructions, or procedures. This can be done from the Logo prompt or an Editor, which is invoked by EDALL in many Logo dialects. The commands TO CHAIR and END must be entered on separate lines.
EDALL
TO CHAIR
REPEAT 4 [FD 100 RT 90] FD 200
END
Once one is finished with the editor, one must exit from it. The new word is saved into the available vocabulary, but the definition will be lost once the Logo interpreter is stopped. In this case, any time CHAIR is entered, "REPEAT 4 [FD 100 LEFT 90] FD 200" will be executed. CHAIR can be used as a command; for example, REPEAT 4 [CHAIR] would compound the <code>CHAIR operation four times.</code>
The turtle can erase a line, using the command PENERASE PE. The pen can be replaced with the command PENPAINT PPT.
EDALL ;(to enter the editor mode, then the actual procedure)
TO ERASECHAIR
PE
REPEAT 4 [FD 100 RT 90] FD 200
PPT
END
This example introduces two new instructions, which are best taught by running them through a Logo interpreter and observing the result. This typifies the general spirit of Logo.
Logo can pass extra information to its words, and return information. We must tell the word to expect something and give it a name. Notice the use of the colon. We are passing 'by value' and the colon is pronounced as 'the value of'. When the procedure is run with a command like CHAIR 200, the size takes the value 200 so we go 'FD the value of 200'.
EDALL (to enter the editor mode, then the actual procedure)
TO CHAIR :thesize
REPEAT 4 [FD :thesize RT 90] FD :thesize FD :thesize
END
Try
If you do need help. Type HELP, or HELP "HOME ( note the single quote mark.)
Logo is an interpreted language. It is not case dependent , but retains the case used for formatting. It is written in lines . It is a compromise between a sequential programming language with block structures, and a functional programming language. There is no 'standard' LOGO, but UCBLogo is highly regarded. It is a teaching language but its list handling facilities make it remarkably useful for producing useful scripts.
Each line is made up of 'function calls'. There are two types
A command is similar to a Pascal procedure, and a operation is similar to a Pascal function.
A special subset of operations called predicates, that just output the word "true or "false, these are conventionally written with a final ‘p’- like emptyp, wordp, listp.
Mathematics in Logo uses prefix notation, like: sum :x :y, product :x :y, difference :x :y, quotient :x :y. Infix is also available.
A command can call itself, this is called recursion
There are three datatypes in UCBLogo,
A number is a special case of word.
There is no strong typing. The interpreter detects the datatype by context.
There are two important symbols
:- this means 'the contents of'
This is an extremely useful symbol that keeps reminding students that a variable is really some 'place' in memory.
A number is a special case of self evaluation- it really could be written with a quote 2 is really "2
Assignment in Pascal x:= y +3 becomes in Logo
or
make takes 2 parameters, the second of which here is sum :y "3. Now sum takes two 'parameters' and is a 'operation', thus the calculation is possible. "3 evaluates to 3, and :y takes the contents of the thing called y, these are summed giving a number. The effect of make is to place the result into the first parameter.
An alternative way of looking at this, maybe, is that the second parameter is 'passed by value' while the first is 'passed by address.'
Indirection (within a procedure) is possible with the form make :x :x + 1.
Variables don’t have to be declared before use. Their scope is then global.
A variable may be declared local, then its scope is limited to that procedure and its subprocedures. This is dynamic scoping. Calling a 'procedure' with 'inputs', creates 'local variables' which hold the contents of the parameters.
Discussing lists comes as a surprise to a Pascal programmer, who has managed quite well without them, however this opens many new possibilities. Arrays are also provided for the timid.
a: One way would be to use iteration.
b: Another, more elegant way would be
This method uses recursion, and is an example of a 'functional' rather than a 'sequential' programming approach.
The standard Pascal controls are available, there is selection
There are iteration commands
Recursion is Logo preferred processing paradigm.
The Pascal programmer will be surprised by a series of list based control structures. The basic idea is that you have two lists
each of the commands is applied in turn to each of the data items. There are several of these template commands with names like MAP, APPLY, FILTER, FOREACH, REDUCE and CASCADE. They repesent four flavours of template iteration, known as explicit-slot, named-procedure, named-slot (or Lambda) and procedure-text.
A property list is a special list where the odd number items are property name, and the even are property values. There are three commands to process property list.
Text may be written to the command window (output stream) using print, show and to the graphics window using label
The standard commands are readlist readword readchar with the normal input stream being the keyboard. In Unix tradition the input stream can be changed, so input can come from a disk file. Similarly, output can be redirected. The technique will be familiar to Pascal Programmers- using a sequence
There are equivalent commands to change the output stream, openwrite, openappend, setwrite, setwritepos nn.
Creates a transcript of everything that is typed in or outputted to the command window.
This turns it off.
Turtle graphics is a powerful method of introducing thinking but LOGO also has a few useful Cartesian commands
This is a typical garden dial. The graphic can be printed and transferred to wood or brass to make an accurate garden timepiece.
A sundial plate must be calculated for its latitude using the formula
The Gnomon Angle = 90 - latitude.
This dial is set for 38N, the latitude of Berkeley, California- the home of UCBLogo. A small correction should be made for Longitude.
MSWLogo supports multiple turtles, and 3D Graphics. MSWLogo allows input from COM ports and LPT ports and also 'hardware' ports. MSWLogo also supports a windows interface thus I/O is available through this GUI- and keyboard and mouse events can trigger interrupts.