# 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.
   * ifelse test [ do_if_true list ] [do_if_false list]
There are iteration commands
   * while condition [instruction list]
   * until condition [instruction list]
   * repeat number [instruction list]
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
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.

### 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.

### I/O Commands

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

### Graphics

Turtle graphics is a powerful method of introducing thinking but LOGO also has a few useful Cartesian commands

#### 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
The Gnomon Angle = latitude.
    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.
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.