A Wikibookian believes this page should be split into smaller pages with a narrower subtopic. You can help by splitting this big page into smaller ones. Please make sure to follow thenaming policy. Dividing books into smaller sections can provide more focus and allow each one to do one thing well, which benefits everyone. |
A reader requests that the formatting and layout of this book be improved. Good formatting makes a book easier to read and more interesting for readers. SeeEditing Wikitext for ideas, andWB:FB for examples of good books. Please continue toedit this book and improve formatting, even after this message has been removed. See thediscussion page for current progress. |
Logo inherits lists fromLisp, and they are its primary method of storing vectors. Arrays are also provided.
One way would be to use iteration.
to firstfive :alist ifelse lessp count :alist 6 [ op :alist ][make "olist []] repeat 2 [ make "olist lput first :alist :olist make "alist bf :alist ] output :olist end show firstfive [1 2 3 4 5 6 7 8 9 ] [1 2 3 4 5] foreach firstfive [1 2 3 4 5 6 7 8 9 ] [show 10 - ?] 9 8 7 6 5
Another, more elegant way would be
to firstn :num :list if :num = 0 [output []] output fput (first :list) (firstn :num-1 butfirst :list) end to firstfive :list output firstn 5 :list end
This method uses recursion, and is an example of a functional rather than an imperative programming approach.
And a more general way would be
to tail :num :alist if or not numberp :num not listp :alist [print [Error: Bad argument(s)] stop] if not equalp 0 :num - int :num [make "num int :num] if :num=0 [output []] if equalp count :alist abs :num [op :alist] if lessp count :alist abs :num [output :alist] if :num>0 [output tail.left :num :alist] if :num<0 [output tail.right abs :num :alist] end to tail.left :num :list output fput (first :list) (tail.left :num-1 butfirst :list) end to tail.right :num :list output lput (last :list) (tail.right :num-1 butlast :list) end show tail 5 [1 2 3 4 5 6 7 8 9] [1 2 3 4 5] show tail -5 [1 2 3 4 5 6 7 8 9] [5 6 7 8 9]
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.
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.
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 listremprop :listname :name :value ;to remove a pair to the listshow gprop :listname :name ;to get the matching value from the list
Text may be written to the command window (output stream) usingprint, show and to the graphics window usinglabel
The standard commands arereadlist 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.
openread [filename]setread[filename]setreadpos nnreadcharsetread[]close [filename].
There are equivalent commands to change the output stream,openwrite, openappend, setwrite, setwritepos nn.
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 inASCII art conversion programs. An example of this, a set of crosses resembling theMandelbrot set.
Turtle graphics is a powerful method of introducing thinking but LOGO also has a few usefulCartesian 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)
This is a typical garden dial. The graphic can be printed and transferred to wood or brass to make an accurate garden timepiece.
to dial cs show [Type in your latitude as a whole number] make "latitude readword ;uses keyboard input for [i 0 6 1][ make "ang arctan product sin :latitude tan product :i 15 ;the calculation rt :ang fd 200 bk 200 lt :ang ;draw the morning line lt :ang fd 200 bk 200 rt :ang ;use symmetry to draw the afternoon line ] pu setx -300 sety -300 seth 90 pd ;send the turtle to the bottom fd 300 seth 270 rt 90 - :latitude fd 300 ;draw the [[style]] or [[gnomon]] pu home pd ;tidy it up end
The same code in a different Logo dialect (in this case, NetLogo, would look like this.
to dial clear-all create-turtles 1 let latitude 38 ;Just set this manually let ang 0 ask turtles [pu set heading ang] foreach [0 1 2 3 4 5 6][ set ang atan ((sin latitude) * (tan (? * 15))) 1 ;the calculation ask turtles [pd rt ang fd 10 bk 10 lt ang ;draw the morning line lt ang fd 10 bk 10 rt ang pu ;use symmetry to draw the afternoon line ] ] ask turtles [pu setxy -15 -15 set heading 90 pd ;send the turtle to the bottom fd 15 set heading 270 rt (90 - latitude) fd 15 ;draw the [[style]] or [[gnomon]] pu home pd ;tidy it up ] end
A sundial plate must be adjusted by latitude using the formula
x= arctan( sin(latitude)*tan(HourDiff * 15) )
The Gnomon Angle = latitude.
This dial above is set for 38N, the latitude of Berkeley, California. A small correction should also 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.
Simple GIF animations may also be produced on MSWlogo version 6.5 with thegifsave 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):

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 :newfilenameendMake "generation 0Make "newname "reproducer1.lgoMake "startup [clone]