Professional Documents
Culture Documents
www.nvcc.edu/home/vfitton/logo
Contents Programming fundamentals 1: Sequence 2: Repetition 3: Subprograms 4: Variables 5: Decision-making Strings Recursive procedures Turtle geometry Color in Logo Using code from other people's programs Saving your pictures and programs References 2 4 7 8 9 11 12 13 14 17 18 20
page
Logo programming
If Logo is not already on your computer, you can get it for free from its makers at
www.softronix.com/mswlogo.html
The installation process puts a Logo icon (as on the cover of this document) on your computer desktop. Here's the MSW Logo screen in two parts: drawing window above, with triangle-shaped TURTLE in center Commander window below
Write commands in command line, i.e., text box at bottom of Commander window. Press Enter or click Execute to run command written there. It's OK to write and run more than one command on line at a time. Command history appears in gray box. Click a line in the history to make it jump to the command line, then make changes (or not) then press Enter or click Execute.
page
or
fd 50 right 90 or
go forward 50 steps
or
rt 90
fd 100 rt 90 fd 100 rt 90
makes two sides of a square several steps on command line are OK left 30 or lt 30 left turn 30 degrees back 100 or bk 100 go backward 100 steps clearscreen or cs erases all drawings and sets turtle at center useful at *beginning* of multi-step commands!
page
BASICALLY HANDY
clearscreen or cs erases all drawings and sets turtle at center useful at *beginning* of multi-step commands! pick pen up, penup or pu so you can move turtle without leaving tracks pendown or pd put pen down hideturtle or ht hides the turtle so you can admire your drawing showturtle or st shows the turtle so you can see what you're doing setpensize [3 3] makes pen larger, easier to see! default pen size is [1 1] label [whatever] writes whatever at cursor location text goes in direction that turtle is pointing wait 20 put this between commands to slow turtle down so you can see what it's doing, for example: fd 100 rt 90 wait 20 fd 100
page
Often we have repeat within repeat. (This is called nesting.) What does this do? Think about it, then try it:
page
All of these drawings use the repeat command. The burst shape at the center used the penup and pendown commands. In between the drawings, I picked up the pen and moved the turtle.
page
page
square
and Logo looks up your commands for doing a square. Click the button that says Edall (for edit all) to bring up Logo's built-in editor. (If your Logo doesn't have an Edall button, write edall on the command line.) Here is the required structure of the subprogram:
page
page
to spiral make "n 1 while [:n < 20] [ ; what goes here?? make "n :n + 1 ] end
The code above shows several new features of the syntax of MSW Logo. We set a variable to a new value by saying make, then the variable's name preceded by a double quote " rather than a colon :
make "n 1
We use a variable, though, with a colon : in front of its name.
page
10
to randomwalk repeat 100 [ make "r random 3 if :r = 0 [fd 20] if :r = 1 [rt 90 fd 20] if :r = 2 [lt 90 fd 20] ] end
This code shows if statements, that have code executed only when a given condition is true. It also shows a Logo built-in that generates random numbers. The statement random 3 produces a 0, 1, or 2. The procedure then decides which way to go "at random." Can a random walk through the business pages produce results as good as a stockbroker's? The statement random 6 produces a number chosen from 0, 1, 2, 3, 4, 5. So what do you say to make Logo roll dice?
For a more random walk than this, see the program bug on my web site.
page
11
Strings
In computer science, any sequence of characters like this goes by the name of string. Dealing with strings is fundamental: for example, how does the computer understand the commands that we give it? It has to break them into pieces and figure out what the pieces mean according to what they are and where they are. Counting the characters is the most basic of all string processes. The answer to the question howlong "abcdefg is given by the following procedure:
to howlong :s make "count 0 ; why zero? while [not emptyp :s] [ make "count :count + 1 print first :s ; it's helpful to see it make "s butfirst :s ; butfirst means all but ; the first of something ] print (sentence :s "has :c "letters) end
The command print writes a result on the command line, and we can see a sequence of results in the command history. When you're writing and troubleshooting a procedure like this, it's useful to show yourself the intermediate values, as we do in the statement print first :s. It wouldn't hurt to print butfirst :s at this stage, too. Here's a much more challenging task: use the new ideas shown above to make a procdure that counts the number of occurrences of a character within a string. For example, the command howmany "a "yabbadabba would give the result 4.
page
12
Recursive procedures
Can a procedure call itself? Why not? When it does, it is called a recursive procedure because the call recurs there's a recurrence of the procedure within the procedure.
This pleasing picture was produced by the following procedure with the call spiral 50 :
page
13
Turtle geometry
Many programming systems work on the same kind of two-axis xy coordinate plane that you work with in algebra.
x is horizontal y is vertical
0 0 is the center, or origin (no comma or parentheses here!) In its centered, zoom-"normal" state, Logo's drawing screen shows an area about 150 points up or down and 300 points right or left from the center. The turtle can be directed with headings that correspond to a compass rose, with 0 or 360 degrees pointing straight up, 90 degrees straight to the right, and so on. You can set a variable to a number between 0 and 360 and then walk that way.
TURTLE GEOMETRY
setx 100 set turtle's x-coordinate to +100 move it 100 points to right of center no vertical change setx -200 move turtle 200 points to left of center no vertical change sety 150 set turtle's y-coordinate to 150 move it 150 points above center no horizontal change sety -50 move turtle 50 points below center no horizontal change setxy 100 100 move turtle to xy coordinate 100 100 show xcor report on command line: show ycor where is the turtle now? cs sety 100 think about it setx 100 try it! sety 0 setx 0 setheading 0 point turtle straight up, "high noon," "due north" or seth 0 seth 120 four o'clock
page
14
Color in Logo
Computer screens work with red, green, and blue components of light, so they are sometimes called RGB screens. (Remember Roy G. Biv?) On Logo's Set menu, you can set the color of three screen elements the turtle's pen the turtle's fill (like a paint bucket for enclosures) the screen background
You set a color by moving these three sliders left and right. Recall that black is the absence of all color and white is all colors together. Mixing light isn't like mixing paint. If you mix red and green paint, you get a muddy color what happens if you mix red and green light? Since this is a computer, every color has an internal numeric representation. The left end is of the sliding scale 0, zero. The right end is 255, which is kind of like 99 to a computer. (It's 28 - 1.) Thus black is [0 0 0], red is [255 0 0], green is [0 255 0], blue is [0 0 255], you can make anything in between that you like, and in all there are 256 * 256 * 256 possible colors. That's 28 * 28 * 28 , or 24 bits of color 24 binary digits inside the machine. These commands give you a big fat red pen:
page
15
When you find a color you like using sliders, you can ask Logo what it is: choose the pen color, then in the command window, enter
show pencolor
You can make a colored square like this: draw the square pen up go to a point inside the square fill
setpencolor [r g b] setpc [r g b] setfloodcolor [r g b] setfc [r g b] setscreencolor [r g b] setsc [r g b] show pencolor show floodcolor show screencolor fill
color for turtle's pen r g b are numbers in range [0, 255] color for an enclosed area color for background tells you what values are right now for [r g b] of named item
dumps a bucket of current floodcolor at cursor's location setpensize [w h] sets width and height of pen w h are numbers in range [1, 5]
page
16
setpencolor yellow
When Logo sees the word yellow, it looks in the list of procedures that it knows, finds your procedure for yellow, and sets the pen color accordingly. Make a blue square with yellow inside:
setpc blue repeat4 [fd 100 rt 90] rt 45 penup fd 25 setfc yellow fill
page
17
want all or some? all of it 2: browser Control-A on keyboard to Select All 3: browser right-click and drag with mouse to highlight selection just some
if editor finds errors, check: duplicate procedure names? one procedure starts before another ends?
8: MSW Logo 6: Editor (in Logo) position cursor with care, then Control-V to paste enter new procedure name on command line and run!
happy ending
People who know Windows well can think of half a dozen ways to do the same thing, but I find this one reliable and straightforward.
If your Logo doesn't have an Edall button (box 5), write edall on the command line.
page
18
no
you can also resize the area using the method for too small, but its easier to crop using Paint
7: Paint with Selection tool, draw box around desired part of picture
8: Paint
you need to figure out the xy coordinates of the desired area
6: MSW Logo 3: Desktop or other folder right-click the file you just saved, then Open With >> Paint change the coordinates from the default, then try again from the top 9: Copy To save under a new filename
Other bitmap formats are more compact; the bitmap files created by Logo are huge. You can conserve space and time, in case you send your drawings over the Internet, by converting your bitmap to another format. Two programs you could use for this are the Windows accessory program Paint and the wonderfully useful IrfanView. Copy and paste, then use the Save As menu of the other program to select a format.
page
19
You will want to save the Logo procedures that you write, either to show off for other people or to continue refining them. (Probably both.) Like other programs, Logo saves nothing until you tell it to. A feature of MSW Logo is that before you save, it remembers your work in two ways, as well as in two distinct locations: your command-line commands are remembered in the command history your procedures, i.e., the words you added to Logo, are remembered in the editor When you do File menu/ Save, only the procedures are saved. If you want to remember the commands as well, then you must copy and paste them into the editor. .LGO is the filename extension for files of procedures created by MSW Logo.
save Logo procedures
MSW Logo saves only code thats in the procedure editor
8: MSW Logo
whew!
yes, lots of them including one granddaddy that calls all the others
no...
copy commands from command history to editor
happy ending
5: Editor position cursor with care, then Control-V to paste code into place 7: Editor File menu Save and Exit
You can also use the Windows clipboard to copy and paste your procedures from the Logo command history or the Logo editor into an e-mail to yourself. Later, you will need to copy and paste them from the e-mail back into Logo's editor.
page
20
References
Here are a few of many great sources for more information about Logo. Harvey, B. Computer Science Logo Style, second edition. Cambridge, Massachusetts: MIT Press, 1997. In this amazing three-volume book, Brian Harvey teaches computer science for adults, not children using Logo. This work will be of particular interest to someone who wants to approach computer science using functional programming, as opposed to structural (e.g., Pascal) or object-oriented (e.g., Java). All 1000 pages can be downloaded from the author's web page at www.cs.berkeley.edu/~bh. Harvey is one of the authors of Berkeley Logo, available on a variety of platforms, including Linux. (MSW Logo is based on Berkeley Logo.) He has also written a beginning computer science book using the functional language Scheme. And here are some web sites, each with more links. (Use Google if the link is broken.) MSW Logo, created by George Mills www.softronix.com/logo.html Fabulous freeware with links to many other sites. A Turtle for the Teacher, by Paul Dench www.ecu.edu.au/pa/ecawa/sig/logo/paul_dench/turtle/ Detailed and lengthy, at an elementary level. Simone Rudge's college-level course outline www.yukoncollege.yk.ca/~srudge/comp052/notes.html Likewise, at a college level. Logo Art Gallery by Yehuda Katz www.geocities.com/CollegePark/Lab/2276/ Lots of arty and recursive examples. Logo Foundation at MIT el.media.mit.edu/logo-foundation/index.html MIT's Artifical Intelligence Lab and Professor Seymour Papert have been instrumental in the development of Logo and the "learn by making" philosophy that Logo embodies. The Logo Foundation's What Is Logo page suggests the breadth and depth of Logo development around the world.