Logo Programming

Logo inherits lists from Lisp, and they are its primary method of storing vectors. Arrays are also provided.


 * Operators exist to convert words into lists, and lists into arrays and back again.
 * This data type has the advantage over arrays that it is infinitely expandable. Data are extracted using the operations first, butfirst, last, butlast, butmember, member and item. Data elements are added using sentence fput and lput.
 * A list can be considered to be a queue with the operators queue and dequeue, or a stack with the operations push and pop.
 * Recursion rather than iteration is the natural method to process lists.

Example: using list primitives to extract the first five members of a list
One way would be to use iteration.

Another, more elegant way would be

This method uses recursion, and is an example of a functional rather than an imperative programming approach.

And a more general way would be

Control structure commands
Logo provides several common control structures.



There are iteration commands



Recursion is Logo's preferred processing paradigm.

Template iteration
Logo also provides list-based control structures. The basic idea is that you have two lists

OPERATION [ a list of commands ] [ many data items ] 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 represent four flavours of template iteration, known as explicit-slot, named-procedure, named-slot (or Lambda), and procedure-text.

show map [? * ? ] [ 5 6 7 ] [25 36 49 ] show filter [ (count ?) > 4 ] [ the quick brown fox jumps over the lazy dog ] [quick brown jumps] show foreach [1 2 3 4 5] [ ? * 10 ] [10 20 30 40 50]

RUN [ list of commands ] ;run a list of commands (or programs) from in a program.

Property lists
A property list is a special list where the odd number items are property names, and the even are property values. There are three commands to process property list. pprop :listname :name :value ;to add a new pair to the list remprop :listname :name :value ;to remove a pair to the list show gprop :listname :name ;to get the matching value from the list

I/O Commands
Text may be written to the command window (output stream) using   and to the graphics window using

The standard commands are  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.

openread [filename] setread[filename] setreadpos nn readchar setread[] close [filename].

There are equivalent commands to change the output stream,

dribble [filename] Creates a transcript of everything that is typed in or outputted to the command window. nodribble This turns it off.

I/O commands can be applied to generate text to resemble a known bitmap, also seen in ASCII art conversion programs. An example of this, a set of crosses resembling the Mandelbrot set.<!-- If this example is important, please supply the Logo code and let the reader generate the output                                                  X                                                                                                X                                                                                                  XX                                                                                                 XXXX XXXXX XXXXX XXX XXXXXX XX XXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX XX                                                                              XXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXX X     XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXX   XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXX   XXXXXXXXXXXXXXXXXXXXXXXXXXXX X     XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX XX                                                                             XX  XXXXXXXXXXXXX XXXXXX XXX XXXXX XXXXX XXXX XX                                                                                                  X                                                                                                      X then a screen dump of the result in MSW or X-logo would be nice! -->

Graphics
Turtle graphics is a powerful method of introducing thinking but LOGO also has a few useful Cartesian commands home        ;returns the turtle to (0,0) setx xx      sety yy      ; sends the turtle, still drawing to (xx,yy) seth nn     ; sets the turtle on a heading or compass bearing of (nn)

Example: calculating and drawing a sundial for a given latitude
This is a typical garden dial. The graphic can be printed and transferred to wood or brass to make an accurate garden timepiece. The same code in a different Logo dialect (in this case, NetLogo, would look like this. A sundial plate must be adjusted by latitude using the formula x= arctan( sin(latitude)*tan(HourDiff * 15) ) The Gnomon Angle = latitude.
 * [[Image:Ex10 38N.jpg|center|Sundial in Berkeley, California]]

This dial above is set for 38N, the latitude of Berkeley, California. A small correction should also be made for longitude.

MSWLogo extensions
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.

GIF animations
Simple GIF animations may also be produced on MSWlogo version 6.5 with the gifsave command. Below is an example of code to generate a simple animation. to squareani :number setactivearea [-5 -5 105 105]        ;we do not want to save the entire screen local "col setpensize [3 3]                      ;make it a noticeable square  make "append "false                   ;signify that we want to start a new animation   repeat :number [    cs    make "col 255*(repcount-1)/(:number-1) setpc (list :col :col :col)        ;change the square's color repeat 4 [                        ; fd 100 rt 90                      ;draw the square ]                                 ;   (gifsave "squareani.gif 5 :append 0) ;save to squareani.gif   make "append "true                   ;from now on, we want to keep adding frames to squareani.gif   ] end Thus, with a :number variable of 15 frames, the result is given below, along with a slightly more complex example (note that this gifsave feature does not yield the same quality color gradients as Logo would save in bitmap form):

Save/Load Functions
MSWLogo and FMSLogo support saving and loading of LOGO procedures and files.

to clone make "generation :generation + 1 make "newfilename (word "reproducer :generation ".lgo) save :newfilename load :newfilename end Make "generation 0 Make "newname "reproducer1.lgo Make "startup [clone]

LOGO