218.186.137.161 (talk) +zh |
ClemRutter (talk | contribs) A major revision- showing the power of the language as proposed in discussion- it needs tidying, more examples and peer review |
||
Line 2: | Line 2: | ||
'''Logo''' is a computer [[programming language]] designed by [[Seymour Papert]] in the late [[1960s]] to teach computer skills to children. Papert is a professor at [[MIT]], and the author of ''Mindstorms: Children, Computers, and Powerful Ideas''. Logo is from the Greek word "logos" meaning "word". |
'''Logo''' is a computer [[programming language]] designed by [[Seymour Papert]] in the late [[1960s]] to teach computer skills to children. Papert is a professor at [[MIT]], and the author of ''Mindstorms: Children, Computers, and Powerful Ideas''. Logo is from the Greek word "logos" meaning "word". |
||
Papert used [[Lisp programming language|LISP]] but changed the [[syntax]] so it is much easier to read. One could say that Logo is Lisp without the parentheses. |
Papert used [[Lisp programming language|LISP]] but changed the [[syntax]] so it is much 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 having their own strengths. A popular Linux implementation is UCBLogo and MSWLogo its freeware Windows derivative is commonly used in UK schools, Comenius Logo is available in Dutch, German, Czech et c. and is worth considering. |
|||
==Turtle Graphics== |
|||
==Logo Programming== |
|||
One of its most notable features was called "Turtle Graphics"; a system for creating graphics that allowed a Logo user to easily produce visual effects from simple programs. The idea was that a turtle with a pen strapped to it could be instructed to do simple things like move forward 100 spaces or turn around. From these building blocks you could draw more complex shapes like squares, triangles, circles--using these to draw houses or sail boats. |
|||
The idea is that a turtle with a pen strapped to it could be instructed to do simple things like move forward 100 spaces or turn around. From these building blocks you could build more complex shapes like squares, triangles, circles--using these to draw houses or sail boats. |
|||
The turtle moved with commands that were 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 she would do if she were the turtle. Papert called this "body syntonic" reasoning. |
The turtle moved with commands that were 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 she would do if she were the turtle. Papert called this "body syntonic" reasoning. |
||
The idea of turtle graphics is also useful for |
The idea of turtle graphics is also useful for |
||
example in [[Lindenmayer system]] for generating fractals. |
example in [[Lindenmayer system]] for generating fractals. |
||
===Turtle Programming=== |
===Turtle Programming=== |
||
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. |
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 "LEFT 90" is written "LT 90" <br> |
||
Key words are usual written in UPPER CASE for beginners, but more advanced texts use lower case. |
|||
====Example 1: A Square==== |
====Example 1: A Square==== |
||
Line 26: | Line 28: | ||
FORWARD 100</code> |
FORWARD 100</code> |
||
This would draw a square with sides 100 units long. |
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). |
||
====Example 2: A Triangle==== |
====Example 2: A Triangle==== |
||
The commands may be written on one line, or more |
|||
<code>FORWARD 100 |
|||
RIGHT 120 |
<code>FORWARD 100 RIGHT 120 |
||
FORWARD 100 |
FORWARD 100 RIGHT 120 FORWARD 100</code> |
||
RIGHT 120 |
|||
FORWARD 100</code> |
|||
Would draw a triangle. |
Would draw a triangle. |
||
====Example 3: Dotted Line==== |
====Example 3: Dotted Line==== |
||
The turtle's pen could be lifted and lowered; drawing a dotted line was rudimentary. In this example we'll use the |
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 <code>FORWARD</code>, which is <code>FD</code>. When teaching this say "FD space 10" is saves alot of frustration. Anything written after the ; (semicolon) is ignored so can be used as a comment. |
||
FD 10 ;(drawing a line and moving) |
|||
<code>FD 10 </code> |
|||
PENUP ;(now we've lifted the pen so it won't draw anything even if we do move) |
|||
(drawing a line and moving) |
|||
FD 10 ;(not drawing but moving) |
|||
<code>PENUP </code> |
|||
(now we've |
PENDOWN ;(now we've lowered the pen so it draws a line wherever the turtle moves) |
||
FD 10 ;(drawing a line and moving) |
|||
<code>FD 10 </code> |
|||
PENUP |
|||
(not drawing but moving) |
|||
FD 10 ;(etc...) |
|||
<code>PENDOWN </code> |
|||
PENDOWN |
|||
(now we've lowered the pen so it draws a line wherever the turtle moves) |
|||
FD 10 |
|||
(drawing a line and moving) |
|||
<code>PENUP |
|||
FD 10 </code> |
|||
(etc...) |
|||
<code>PENDOWN |
|||
FD 10</code> |
|||
====Example 4: Loops==== |
====Example 4: Loops==== |
||
Line 68: | Line 61: | ||
Which would execute the command "<code>FD 100 RIGHT 90</code>" four times. |
Which would execute the command "<code>FD 100 RIGHT 90</code>" four times. |
||
A simplistic circle consists of 360 individual rotations with a step forward, so "<tt>REPEAT 360 [FD 1 RIGHT 1]</tt>" would have the expected result. |
A simplistic circle consists of 360 individual rotations with a step forward, so "<tt>REPEAT 360 [FD 1 RIGHT 1]</tt>" would have the expected result. |
||
====Example 5: |
====Example 5: New words==== |
||
You |
You can teach the turtle new words, i.e. groups of instructions,or procedures. These can to be done from the Editor, that was known as <cite>EDALL</cite>. This may vary with other dialects of LOGO. <br> 'TO CHAIR' must be on a separate line. 'END' must be on a separate line. Anything written after a ';' is ignored- so can be used as a comment. |
||
<code>ED</code> (to enter the editor mode, then the actual procedure) |
|||
<code>EDALL</code> |
|||
<code> |
<code> |
||
TO |
TO CHAIR |
||
REPEAT 4 [FD 100 RT 90] |
REPEAT 4 [FD 100 RT 90] FD 200 |
||
FD 30 |
|||
END |
END |
||
</code> |
</code> |
||
If you are using the editor you must exit from it, and the new word is saved into the avalabe vocabulary, but will be loost when LOGO is exited. |
|||
Now any time <code>BOBSAGET</code> is entered, "<code>REPEAT 4 [FD 100 LEFT 90] FD 30</code>" will be executed. You could compound these by <code>REPEAT 4 [BOBSAGET] if you wanted.</code> |
|||
Now any time <code>CHAIR</code> is entered, "<code>REPEAT 4 [FD 100 LEFT 90] FD 30</code>" will be executed. You could compound these by <code>REPEAT 4 [CHAIR] if you wanted.</code> |
|||
====Example 6: Erasing ==== |
|||
The turtle can erase a line. <code>PENERASE PE</code>. You must then replace the 'pen' with the command <code>PENPAINT PPT</code>. |
|||
<code>EDALL</code> ;(to enter the editor mode, then the actual procedure) |
|||
<code> |
|||
TO ERASECHAIR |
|||
PE |
|||
REPEAT 4 [FD 100 RT 90] FD 200 |
|||
PPT |
|||
END |
|||
</code> |
|||
====Example 7: Parameters- giving the word changeable information ==== |
|||
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 <code>CHAIR 200</code>, the size takes the value 200 so we go 'FD the value of 200'. |
|||
<code>EDALL</code> (to enter the editor mode, then the actual procedure) |
|||
TO CHAIR :thesize |
|||
REPEAT 4 [FD :thesize RT 90] FD :thesize FD :thesize |
|||
END |
|||
Try |
|||
REPEAT 50 [HOME CHAIR 200 HOME CHAIR 100 WAIT 200 HOME ERASECHAIR ] |
|||
There were two new instructions- which are easier to use than explain. Thats the spirit of LOGO. <br> |
|||
If you do need help. Type <code>HELP</code>, or <code>HELP "HOME</code> ( note the single quote mark.) |
|||
==The Language== |
|||
Logo is an interpreted language. It is not case dependant , but retains the case used for formatting. It is written in lines . It is a teaching language but its list handling facilities make it remarkable useful for producing useful scripts. |
|||
===Functions/Procedures=== |
|||
Each line is made up of 'function calls'. There are two types |
|||
* commands (which do something ‘effects’ but don’t return a value) like print |
|||
* operations ( which just return a value, its output) like sum, first or readlist. |
|||
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 <code>"true</code> or <code>"false</code>, these are conventionally written with a final ‘p’- like <code>emptyp, wordp, listp</code>. |
|||
* Expressions can be primitives, or can be defined by the user. |
|||
* Expressions can take zero, one or more parameters. |
|||
Mathematics in Logo uses prefix notation, like: <code>sum :x :y, product :x :y, difference :x :y, quotient :x :y<code>. Infix is also available. |
|||
help "keyword ;(will bring up a full description of the expression) . |
|||
A command can call itself, this is called [[recursion]] |
|||
====Example 8: A spiral drawn using recursion==== |
|||
to spiral :size |
|||
if :size > 30 [stop] ; a condition stop |
|||
fd :size rt 15 ; many lines of action |
|||
spiral :size *1.02 ; the tailend recursive call |
|||
end |
|||
spiral 10 |
|||
===Data=== |
|||
There are three datatypes, |
|||
* the word, |
|||
* the list, |
|||
* the array. |
|||
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 |
|||
* The colon <code>:</code>- this means 'the contents of' |
|||
This is an extremely useful symbol that keeps reminding students that a variable is really some 'place' in memory. |
|||
* The quote- this means '"the word is evaluated as itself"', or '"its value after evaluation is the same as it was before"'. This is important. |
|||
A number is a special case of self evaluation- it really could be written with a quote '''2''' is really '''"2''' |
|||
Assignment in Pascal <code>x:= y +3</code> becomes in Logo |
|||
make "x sum :y 3 |
|||
or |
|||
make "x sum :y "3 |
|||
<code>make</code> takes 2 parameters, the second of which here is <code>sum :y "3</code>. Now <code>sum</code> takes two 'parameters' and is a 'operation', thus the calculation is possible. <code>"3</code> evaluates to <code>3</code>, and <code>:y</code> takes the contents of the thing called <code>y</code>, these are summed giving a number. The effect of <code>make</code> 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 <code>make :x :x + 1</code>. |
|||
====Scoping==== |
|||
Variables don’t have to be declared before use. Their scope is then global. |
|||
A variable may be declared <code>local</code>, thenits 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. |
|||
===Lists=== |
|||
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. |
|||
* Operators exist to convert words into lists, and lists into arrays and back again. |
|||
* This data type has the advantage over array in that it is infinitely expandable Data is extracted using the the operations first, butfirst, last, butlast, member and item. Data is 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 9: Using List primitive to extract the first 5 members of a list==== |
|||
to first5 :alist |
|||
ifelse lessp count :alist 5 [ op :alist ][ |
|||
make "olist [] |
|||
repeat 5 [ make "olist lput first :alist :olist make "alist bf :alist ] op :olist ] |
|||
end |
|||
show first5 [1 2 3 4 5 6 7 8 9 ] |
|||
[1 2 3 4 5] |
|||
foreach first5 [1 2 3 4 5 6 7 8 9 ] show 10 - ? |
|||
[9 8 7 6 5] |
|||
===Control Structure Commands=== |
|||
The standard Pascal controls are available, there is selection |
|||
* 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 prefered processing paradigm. |
|||
===Unfamiliar Control structures-- Template Iteration === |
|||
The Pascal programmer will be surprised by a series of 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 <CODE>MAP, APPLY, FILTER, FOREACH, REDUCEand CASCADE.</CODE> They repesent 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 name, 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 <code>print, show</code> and to the graphics window using <code>label </code> |
|||
The standard commands are <code>readlist readword readchar</code> 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 |
|||
openread [filename] |
|||
setread[filename] |
|||
setreadpos nn |
|||
readchar |
|||
setread[] |
|||
close [filename]. |
|||
There are equivalent commands to change the output stream, <code>openwrite, openappend, setwrite, setwritepos nn. <code> |
|||
dribble [filename] |
|||
Creates a transcript of everything that is typed in or outputted to the command window. |
|||
nodribble |
|||
This turns it off. |
|||
===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 10: Calculating and drawing a [[sundial]] for a given latitude==== |
|||
This is a typical garden dial. The graphic can be printed and transfered 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 keyboad 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 |
|||
A sundial plate must be calculated for its latitude using the formula |
|||
x= arctan( sin(latitude)*tan(HourDiff * 15 ) ) |
|||
The Gnomon Angle = 90 - latitude. |
|||
===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. |
|||
==Logo instead of [[Pascal]]?== |
|||
Logo is slow, it is not suitable for processsing vast amount of data- it is a learning language. If speed is not an issue anymore it is educationally more useful to watch a program cranking through a problem. The visual output allows this. It assists students understanding how the computer is working sequentially through the instructions. |
|||
Logo scores on graphics, and MSWLogo allows the use the Windows dll.. A crucial point for learning Pascal is that students must master 'pointers' before they can do any useful data structure theory. This excludes all except the most committed. In Logo, Lists, stacks and queues are trivial. Trees are more fun. All are recursive in nature. |
|||
UCBLogo and MSWLogo include a rudimentary Pascal intrepreter written in Logo as one of the examples. |
|||
==External Link== |
==External Link== |
||
* [http://www.cs.berkeley.edu/~bh/logo.html Berkeley Logo] (UCBLogo, GNU) |
|||
* [http://www.softronix.com MSWLogo]: To down load MSWLogo. |
|||
* [http://www.input.sk/slogo/usage.htm Comenius] A popular European Logo. |
|||
* [http://www.embry.com/rLogo/ rLogo]: Play with Logo online (you will need a [[Java programming language|Java]]-enabled [[Web browser|browser]]). |
* [http://www.embry.com/rLogo/ rLogo]: Play with Logo online (you will need a [[Java programming language|Java]]-enabled [[Web browser|browser]]). |
||
* [http://education.mit.edu/starlogo/ MIT StarLogo] |
* [http://education.mit.edu/starlogo/ MIT StarLogo] |
||
* [http://www.cs.berkeley.edu/~bh/logo.html Berkeley Logo] (UCBLogo, GNU) |
|||
==External Sources== |
|||
Computer Science Logo Style, Brian Harvey, MIT Press (3 volumes) 0-262-58148-5, 0-262-58149-3,0-262-58150-7.<br> |
|||
The Great Logo Adventure, Jim Muller, Doone Publications 0-9651934-6-2 |
Revision as of 03:52, 30 November 2003
[[pl:J%EAzyk programowania Logo]] Logo is a computer programming language designed by Seymour Papert in the late 1960s to teach computer skills to children. Papert is a professor at MIT, and the author of Mindstorms: Children, Computers, and Powerful Ideas. Logo is from the Greek word "logos" meaning "word".
Papert used LISP but changed the syntax so it is much 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 having their own strengths. A popular Linux implementation is UCBLogo and MSWLogo its freeware Windows derivative is commonly used in UK schools, Comenius Logo is available in Dutch, German, Czech et c. and is worth considering.
Logo Programming
The idea is that a turtle with a pen strapped to it could be instructed to do simple things like move forward 100 spaces or turn around. From these building blocks you could build more complex shapes like squares, triangles, circles--using these to draw houses or sail boats.
The turtle moved with commands that were 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 she would do if she 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.
Turtle Programming
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 "LEFT 90" is written "LT 90"
Key words are usual written in UPPER CASE for beginners, but more advanced texts use lower case.
Example 1: A Square
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).
Example 2: A Triangle
The commands may be written on one line, or more
FORWARD 100 RIGHT 120
FORWARD 100 RIGHT 120 FORWARD 100
Would draw a triangle.
Example 3: Dotted Line
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
. When teaching this say "FD space 10" is saves alot of frustration. Anything written after the ; (semicolon) is ignored so can be used as a comment.
FD 10 ;(drawing a line and moving) PENUP ;(now we've lifted the pen so it won't draw anything even if we do move) FD 10 ;(not drawing but moving) PENDOWN ;(now we've lowered the pen so it draws a line wherever the turtle moves) FD 10 ;(drawing a line and moving) PENUP FD 10 ;(etc...) PENDOWN FD 10
Example 4: Loops
You could also use loop (repeat) commands. This would draw the exact same box 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.
Example 5: New words
You can teach the turtle new words, i.e. groups of instructions,or procedures. These can to be done from the Editor, that was known as EDALL. This may vary with other dialects of LOGO.
'TO CHAIR' must be on a separate line. 'END' must be on a separate line. Anything written after a ';' is ignored- so can be used as a comment.
EDALL
TO CHAIR REPEAT 4 [FD 100 RT 90] FD 200 END
If you are using the editor you must exit from it, and the new word is saved into the avalabe vocabulary, but will be loost when LOGO is exited.
Now any time CHAIR
is entered, "REPEAT 4 [FD 100 LEFT 90] FD 30
" will be executed. You could compound these by REPEAT 4 [CHAIR] if you wanted.
Example 6: Erasing
The turtle can erase a line. PENERASE PE
. You must then replace the 'pen' 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
Example 7: Parameters- giving the word changeable information
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
REPEAT 50 [HOME CHAIR 200 HOME CHAIR 100 WAIT 200 HOME ERASECHAIR ]
There were two new instructions- which are easier to use than explain. Thats the spirit of LOGO.
If you do need help. Type HELP
, or HELP "HOME
( note the single quote mark.)
The Language
Logo is an interpreted language. It is not case dependant , but retains the case used for formatting. It is written in lines . It is a teaching language but its list handling facilities make it remarkable useful for producing useful scripts.
Functions/Procedures
Each line is made up of 'function calls'. There are two types
- commands (which do something ‘effects’ but don’t return a value) like print
- operations ( which just return a value, its output) like sum, first or readlist.
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
.
- Expressions can be primitives, or can be defined by the user.
- Expressions can take zero, one or more parameters.
Mathematics in Logo uses prefix notation, like: sum :x :y, product :x :y, difference :x :y, quotient :x :y
. Infix is also available.
help "keyword ;(will bring up a full description of the expression) .
A command can call itself, this is called recursion
Example 8: A spiral drawn using recursion
to spiral :size
if :size > 30 [stop] ; a condition stop
fd :size rt 15 ; many lines of action
spiral :size *1.02 ; the tailend recursive call
end
spiral 10
Data
There are three datatypes,
- the word,
- the list,
- the array.
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
- The colon
:
- this means 'the contents of'
This is an extremely useful symbol that keeps reminding students that a variable is really some 'place' in memory.
- The quote- this means '"the word is evaluated as itself"', or '"its value after evaluation is the same as it was before"'. This is important.
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
make "x sum :y 3
or
make "x sum :y "3
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
.
Scoping
Variables don’t have to be declared before use. Their scope is then global.
A variable may be declared local
, thenits 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.
Lists
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.
- Operators exist to convert words into lists, and lists into arrays and back again.
- This data type has the advantage over array in that it is infinitely expandable Data is extracted using the the operations first, butfirst, last, butlast, member and item. Data is 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 9: Using List primitive to extract the first 5 members of a list
to first5 :alist
ifelse lessp count :alist 5 [ op :alist ][
make "olist []
repeat 5 [ make "olist lput first :alist :olist make "alist bf :alist ] op :olist ]
end
show first5 [1 2 3 4 5 6 7 8 9 ]
[1 2 3 4 5]
foreach first5 [1 2 3 4 5 6 7 8 9 ] show 10 - ?
[9 8 7 6 5]
Control Structure Commands
The standard Pascal controls are available, there is selection
* 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 prefered processing paradigm.
Unfamiliar Control structures-- Template Iteration
The Pascal programmer will be surprised by a series of 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, REDUCEand CASCADE.
They repesent 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 name, 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 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
openread [filename]
setread[filename]
setreadpos nn
readchar
setread[]
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.
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 10: Calculating and drawing a sundial for a given latitude
This is a typical garden dial. The graphic can be printed and transfered 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 keyboad 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
A sundial plate must be calculated for its latitude using the formula
x= arctan( sin(latitude)*tan(HourDiff * 15 ) )
The Gnomon Angle = 90 - latitude.
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.
Logo instead of Pascal?
Logo is slow, it is not suitable for processsing vast amount of data- it is a learning language. If speed is not an issue anymore it is educationally more useful to watch a program cranking through a problem. The visual output allows this. It assists students understanding how the computer is working sequentially through the instructions.
Logo scores on graphics, and MSWLogo allows the use the Windows dll.. A crucial point for learning Pascal is that students must master 'pointers' before they can do any useful data structure theory. This excludes all except the most committed. In Logo, Lists, stacks and queues are trivial. Trees are more fun. All are recursive in nature.
UCBLogo and MSWLogo include a rudimentary Pascal intrepreter written in Logo as one of the examples.
External Link
- Berkeley Logo (UCBLogo, GNU)
- MSWLogo: To down load MSWLogo.
- Comenius A popular European Logo.
- rLogo: Play with Logo online (you will need a Java-enabled browser).
- MIT StarLogo
External Sources
Computer Science Logo Style, Brian Harvey, MIT Press (3 volumes) 0-262-58148-5, 0-262-58149-3,0-262-58150-7.
The Great Logo Adventure, Jim Muller, Doone Publications 0-9651934-6-2