Computer Science Logo Style
These are notes from a book called Computer Science Logo Style 2nd ed. by Brian Harvey. MIT Press 1997. The book has three volumes and are available online:
I installed Logo on my Lubuntu Linux machine via:
sudo apt install ucblogo
Volume 1: Symbolic Computing
Exploration
The name Logo comes from the Greek word logos, which means “word.” In contrast to earlier programming languages, which emphasized arithmetic computation, Logo was designed to manipulate language–words and sentences.
random N
- generate an integer between 0 and N-1
cleartext
orct
- clears the console screen
A function:
to greet print [Hi. What's your name?] print sentence [How are you,] word first readlist "? ignore readlist print [That's nice.] end
The third line is wild. The syntax is saying make a sentence
from
(a) list 1 (How are you,
), (b) the word
coming from of the first
element we read in via readlist
, and (c) a quoted question mark.
If I resubmit the above, I get greet is already defined
. Later, I’ll
learn how to undefine and redefine I assume.
If you do write a collection of … procedures, you’ll want to save them so that they’ll still be available the next time you use Logo. Certainly you’ll want to save the work you do in later chapters. You can ask Logo to record all of the definitions you’ve made as a workspace file using the
save
command. For example, if you enter the instruction? save "mystuffyou are asking Logo to write a disk file called
mystuff
containing everything you’ve defined. (The next time you use Logo, you can get back your definitions with theload
command.)
I tried this and it worked but I did not know where mystuff
is
saved. (It’s saved in ~/Documents
which is fine. And it’s saved as
Logo text exactly as I entered it which is nice.)
The important questions will not be ones like “what does
Procedures and instructions
Here is an instruction:
? print 17
print
- the name of a procedure. It requires one input. I think the input can be a word, sentence, or list. Or an expression. (But does an expression evaluate to a list?)
There are about 200 built-in procedures called primitive procedures.
In the end, your instructions and the procedures they invoke must be defined in terms of the primitive procedures. Those procedures are not made up of Logo instructions. They’re the things that Logo just knows how to do in the first place.
? print sum 2 3
Logo evaluatd the expression sum 2 3
before passing it to the
print
procedure. Logo invoked the necessary procedure (sum
) to
evaluate the expression.
? print sum 4 product 10 2
The input (expression?) print
deals with is sum 4 project 10 2
and
I guess sum
deals with two inputs and sees 4
and another input
which is an expression (project 10 2
)?
This demonstrates composition of functions.
? print 2 print 3
Is fine because the long expression evaluates to two instructions.
- operation
- is a procedure computes a value and outputs it. For
example,
sum
andproduct
. - command
- is a procedure that does not output a value but instead has some effect such as printing something on the screen, moving a turtle, etc.
- complete instruction
- consists of the name of the command, followed by as many expressions as necessary to provide its inputs.
- expression
- an explicitly provided value such as a number or else the name of an operation followed by as many expressions as necessary to provide its inputs.
When Logo is evaluating instructions, it always interprets unadorned words such as
sum
orhello
as names of procedures.
- quote
- prevent something from being evaluated
A list in brackets evaluates to itself:
? print [How are you?]
The list How are you?
has three members each a word. The square
brackets of a list delimit the list and quote the list.
Logo has words and lists as the two kinds of information Logo can process. An example of either is a single datum.
[How are you?]
is a sentence or a flat list.
A series of questions about a procedure:
- Command or operation? Commands have side effects while operations have return values?
- How many inputs?
- What type of datum must each input be?
- If the procedure is an operation, what is it’s output
Manipulating words and lists
- Taking apart operations
A number is a word, a character is a (short) word, and a word is a word.
first
- take the first member of the list or the first character if a word
last
- take the last member of the list or the last character of the word
? print first 43 --> 4 ? print first "Hello --> H ? print first Hello --> I don't know how to Hello ? print first [1 3 8] --> 1 ? print first 1 --> 1 ? print first 1 3 --> You don't say what to do with 3
It’s nice to see how
"Hello
is just a quoted word. In Common lisp one uses'Hello
.butfirst
(orbf
)- take all but the first elements of the list or word
butlast
(orbl
)- all but the last
? print butfirst "Hello --> ello ? print butfirst [I am a cat] --> am a cat ? print butfirst "H --> An empty word ('"') ? print butfirst [] --> An empty list ('[]')
print item 3 [Run to me]
- Putting together operations
sentence
- an operation that takes two inputs which can be any data at all.
Straightforward usage:
? print sentence "hello "goodbye --> hello goodbye ? print sentence [this is] [a test] --> this is a test ? print sentence "this [is one too] --> this is one too ? print sentence [] [list of words] --> list of words
Sentences are flat lists like
[a flat list is a sentence]
But
sentence
can append two lists (flat or not). If the lists are inputs the outputs.Sentence
is the only primative that treats words the same as single-word lists.? print sentence ["Hello,] "World --> "Hello, World ? print sentence "Hello, "World --> "Hello, World
list
- takes any two datums and makes a list. It really just glues the two things.
? print list [a b c] [d e f] --> [a b c] [d e f] (length 2 list)
word
- takes two inputs both are words
? print word "Hello, "World ---> Hello,World ? print word "this [is a test] ---> Doesn't work
Here is the next example:
print word word last "awful first butfirst "computer first [go to the store, please.] <=> print word word l first "omputer first [go to the store, please.] <=> print word "lo first [go to the store, please.] <=> print "logo"
print word 3 4 --> 34 print word 3 .4 --> 30.4 ????
count
- takes one input of any datum and says how many elements.
show
- takes one thing and shows it which means displays the list if it is a list
? show [aardvark] --> [aardvark] (a list) ? show "aardvark --> aardvark (a word)
- Special forms
? print 2+3 --> 5 (nice as a calculator) ? print sum 2 3 --> 5
Some primitives take extra or fewer inputs via parentheses.
print (sum 2 3 4) --> sum is given 3 inputs; Lisp like show (list) --> list is given zero arguments
sum
product
word
list
sentence
print
? show (sentence "a "dog "likes "treats) --> [a dog likes treats]
(print 1 2 3 4) --> 1 2 3 4
Parentheses are OK as visual aids.
? print word (last "awful) (first butfirst "computer))
Or to show precedence when using infix operators:
? print 2 * (3 + 4)
Variables
to greet :person print sentence "Hello, thing "person <--- also :person print [Pleased to meet you.] end
Call this with
? greet "Henry
thing
- takes one word that is the name of a container (variable)
and outputs whatever datum is in the container. So
thing "person
is the same as:person
. The colon is short forthing "
.