User:Just`Existing/sandbox/UCBLogo
‹The template Manual is being considered for merging.›
This article is written like a manual or guide. (August 2018) |
Paradigms | multi-paradigm:functional educational, procedural, reflective |
---|---|
Family | Lisp |
Designed by | Brian Harvey |
Developers | Dan van Blerkom, Michael Katz, Doug Orleans. Substantial contributions: Freeman Deutsch, Khang Dao, Fred Gilham, Yehuda Katz, George Mills, Sanford Owings, Randy Sargent[1] |
First appeared | 1992 |
Stable release | 6.2.1
/ 31 December 2020 |
Typing discipline | dynamic |
Scope | Dynamic |
Implementation language | C |
Platform | IA-32, x86-64 |
OS | Windows, macOS, Linux |
License | GPL |
Website | people |
Influenced by | |
Lisp | |
Influenced | |
Smalltalk, Etoys, Scratch, NetLogo, KTurtle, Rebol |
UCBLogo, also termed Berkeley Logo, is a programming language, a dialect of Logo, which derived from Lisp. It is the dialect of Logo closest to being a de facto standard. It has the best facilities for handling lists, files, input/output (I/O), and recursion.[2] It can be used to teach most computer science concepts, as University of California, Berkeley lecturer Brian Harvey[3] did in his Computer Science Logo Style trilogy.[4][5][6] It is free and open-source software released under a GNU General Public License (GPL).[7]
Graphical user interface
[edit]UCBLogo has a rudimentary graphical user interface (GUI), so several projects exist that provide a better interface. MSWLogo and its successor FMSLogo, for Microsoft Windows, are commonly used in schools in the United Kingdom and Australia.
Design
[edit]Logo was designed in spirit of low threshold and no ceiling, which enables easy entry by novices and yet meet the needs of high-powered users. Animations require both the ability to draw and to erase shapes. The process is the same, except that in the former, a line is deposited on the display device and in the latter a line is removed. Using the turtle analogy, the turtle's pen must paint, and the turtle's pen must erase. The turtle can be set to erase anything below it, using the command PENERASE (PE), while the pen can be set to start drawing again with the command PENPAINT (PPT), in UCBLogo.
The pen
[edit]The analogy of a turtle with a pen attached to its tail is often used. The turtle's pen can be lifted and lowered, thus drawing a rudimentary dotted line.
An example code:
FD 20 ; draw a line and move
PENUP ; lift the pen so it draws nothing
FD 20 ; move and not draw
PENDOWN ; lower the pen so it draws again
FD 20 ; draw a line and move
PENUP ; lift the pen so it draws nothing
FD 40 ; move and not draw
PENDOWN ; lower the pen so it draws again
RT 20 ; rotate right (clockwise) 20 degrees
Data
[edit]There are three datatypes in UCBLogo:
- the word
- the list
- the array
A number is a special case of word.
There is no static typing. The interpreter detects the datatype by context.
Two important symbols are:
- The colon (
:
) means the contents of. This is an extremely useful symbol that keeps reminding students that a variable is really a place in memory. - The doublequote (
"
) means the word is evaluated as itself, or its value after evaluation is the same as it was before. This is important. For users from other programming languages: the doublequote is not paired as opening and closing quotes.
A number is a special case of self-evaluation; it really could be written with a quote. 2 is really "2
Variable assignment (e.g. x := y + 3
) is handled in Logo with the make
command, as exemplified by these two equivalent statements:
make "x sum :y 3 make "x sum :y "3
make
takes 2 parameters, the second of which here is sum :y "3
. sum
takes two 'parameters' and is an '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. From a programmatical perspective, the first argument to make
is passed by reference, while the second is passed by value.
Scoping
[edit]Variables do not have to be declared before use; their scope is then global.
A variable may be declared local
, then its scope is limited to that procedure and any procedures that it calls, which is termed dynamic scope. Calling a procedure with inputs (the name usually used for arguments in the Logo literature) also creates local variables that hold the argument values.
Lists
[edit]Logo inherits lists from Lisp, and they are its main method to store 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
, anditem
. Data elements are added usingsentence
,fput
, andlput
. - 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.
Control structure commands
[edit]Logo provides several common control structures. There is one conditional structure.
- ifelse test [ do_if_true list ] [do_if_false list]
There are three iteration commands:
- while condition [instruction list]
- until condition [instruction list ]
- repeat number [instruction list]
Recursion is Logo's preferred processing paradigm.
Template iteration
[edit]Logo also provides list-based control structures. The basic idea is of 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.
Property lists
[edit]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 from the list
show gprop :listname :name ;to get the matching value from the list
Input, output
[edit]For input/output (I/O), text may be written to the command window (output stream) using print
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.
Syntax
[edit]Commands may be written on one line, or more. Many commands have mnemonic short forms; for example FORWARD
and RIGHT
are coded FD
and RT
respectively. This makes the input less onerous. Anything written after the ; (semicolon) is ignored, allowing the coder to insert comments.
; draws a square with sides 100 units long
FORWARD 100
LEFT 90
FORWARD 100
LEFT 90
FORWARD 100
LEFT 90
FORWARD 100
LEFT 90
FD 100 RT 120 FD 100 RT 120 ; draws a triangle FD 100 RT 120
The Hello World program in Logo looks like this:
print [Hello World]
Loops
[edit]There are three loop (repeat) commands; REPEAT is one. This draws a square.
REPEAT 4 [FD 100 LEFT 90]
The command FD 100 LEFT 90
is executed four times.
An approximation of a circle can be constructed easily with 360 small rotations and a step forward: REPEAT 360 [FD 1 RIGHT 1]
. Loops may be nested, giving results with little effort.
REPEAT 36 [RT 10 REPEAT 360 [FD 1 RT 1]] FD 25 RT 90
Another example for nested Loops
REPEAT 36 [REPEAT 4 [FD 100 RT 90] RT 10]
Functions and procedures
[edit]Each line is made up of function calls, of which there are two types:
- commands (which usually do something—effects—but do not return a value) like
print
. - operations (which just return a value, its output) like
sum
,first
orreadlist
.
A command is similar to a Pascal procedure, and an operation is similar to a Pascal function. (See also: command-query separation, where a query is an operation in Logo). A special subset of operations, called predicates, which just output the word true
or false
, are conventionally written with a final p
. Examples include emptyp
, wordp
, and listp
.
- Expressions can be primitives, or can be defined by the user.
- Expressions can take zero, one or more parameters.
Procedures can be defined on the command line, using the TO END pair:
TO CHAIR REPEAT 4 [FD 100 RT 90] FD 200 END
However, in some early Logos the procedure is limited to the physical line length of the input device.
All Logos can invoke an Editor, usually by EDALL. In the editor, procedures may be written over many lines, as nothing is interpreted until the edit is complete.
EDALL
TO CHAIR REPEAT 4 [FD 100 RT 90] FD 200 END
The new word is saved into the available vocabulary, but the definition will be lost once the Logo session is over. Internally procedures are words and in this case, any time CHAIR
is entered, the sequence REPEAT 4 [FD 100 LEFT 90] FD 200
will be executed. The word CHAIR
can be used as a command; for example, REPEAT 4 [CHAIR]
would repeat the CHAIR
operation four times.
EDALL ;(to enter the editor mode, then the actual procedure)
TO ERASECHAIR PE BK 200 REPEAT 4 [FD 100 RT 90] PPT END
CS CHAIR WAIT 200 ERASECHAIR
A WAIT delay between the drawing and the erasing can introduce the illusion of motion:
CS REPEAT 20 [CHAIR WAIT 200 ERASECHAIR PENUP FD 20 PENDOWN]
Arguments, parameters
[edit]Logo can pass extra information to its words, and return information. The procedure (word) is instructed to expect something and give that something a name. The colon is used for this purpose. It passes the information by value and the colon is pronounced as the value of. When the procedure is run with a command such as CHAIR 200, the word :thesize
takes the value 200 so when FD :thesize
is executed, the interpreter understands 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 END CS REPEAT 9 [CHAIR 50 RT 20 CHAIR 100 WAIT 50 RT 20]
Other notes
[edit]Mathematics in Logo uses prefix or Polish 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).
Logo allows for recursion, a process where a procedure calls itself.
to spiral :size if :size > 30 [stop] ; an exit condition fd :size rt 15 ; many lines of action spiral :size *1.02 ; the tailend recursive call end
spiral 10
See also
[edit]References
[edit]- ^ Harvey, Brian (1997). Volume 1: Symbolic Computing: Acknowledgments. Computer Science Logo Style. Vol. 1. MIT Press. ISBN 0-262-58148-5. Retrieved 2019-05-06.
{{cite book}}
:|website=
ignored (help) - ^ "Logo Programming Language". Logo Foundation. 2012. Archived from the original on 2013-08-15. Retrieved 2019-05-06.
- ^ Harvey, Brian. "Brian Harvey". Electrical Engineering and Computer Sciences. University of California, Berkeley. Retrieved 2019-05-06.
- ^
Harvey, Brian (1997). Volume 1: Symbolic Computing. Computer Science Logo Style. Vol. 1. MIT Press. ISBN 0-262-58148-5. Retrieved 2019-05-06.
{{cite book}}
:|website=
ignored (help) - ^
Harvey, Brian (1997). Volume 2: Advanced Techniques. Computer Science Logo Style. Vol. 2. MIT Press. ISBN 0-262-58149-3. Retrieved 2019-05-06.
{{cite book}}
:|website=
ignored (help) - ^
Harvey, Brian (1997). Volume 3: Beyond Programming. Computer Science Logo Style. Vol. 3. MIT Press. ISBN 0-262-58150-7. Retrieved 2019-05-06.
{{cite book}}
:|website=
ignored (help) - ^ Harvey, Brian (2008-09-14). "Release 6.0 of Berkeley Logo is now available by anonymous FTP or Web". Electrical Engineering and Computer Sciences. University of California, Berkeley. Retrieved 2019-05-09.
External links
[edit]- Official website
- UCBLogo Source Code Repository
- Experimental Online Logo interpreter (Extended version of former CS61A Programming Project #4. Implements a subset of UCBLogo, lacks turtle graphics.)