
The square brackets have been there since S3, holding the words for pr and
the instructions for repeat. It is time to say what they actually are.
A list is Dr Logo's way of holding several things at once, and once you can take one apart you can write procedures that work on any number of things rather than a fixed few. This is the half of the language that has nothing to do with the turtle.
Four primitives, each taking a list and giving back part of it:
?show first [red green blue]
red
?show last [red green blue]
blue
?show bf [red green blue]
[green blue]
?show bl [red green blue]
[red green]
first and last hand back one item. bf - but first - and bl - but
last - hand back a list with one fewer item in it.
show is used here rather than pr because show keeps the brackets, and the
brackets are the point: you need to see which answers are lists and which are
single items.
None of these changes the list you gave it. They hand back something new.
?show emptyp []
TRUE
?show emptyp [red]
FALSE
?show count [red green blue]
3
emptyp - empty predicate - answers TRUE or FALSE. That is what makes it
useful in an if.
Put those together with the recursion from S14 and you can deal with a list of any length:
to items :list
if emptyp :list [stop]
pr first :list
items bf :list
end
If there is nothing left, stop. Otherwise print the first one, and do the whole thing again with the rest.
The list gets one shorter every time round, which is what makes the emptyp
test eventually true - exactly the role :size + 5 played in the spiral.
to items :list
if emptyp :list [stop]
pr first :list
items bf :list
end
items [red green blue]
Starting from
A fresh boot, at the ? prompt.
What you should see
items defined
red
green
blue

The procedure never mentions three, or colours. Give it a list of nine things and it prints nine.
first to last and bf to bl. Run it on the same list. What
order do you get, and why is that the only thing that changed?items [] - the empty list. Then items [only]. Both work. Which line of
the procedure is doing the work in each case?if line out and run items [one two]. It prints both and then
complains. Read the message: which primitive objected, and what was it handed?
That tells you what the if was really protecting you from.| Symptom | Cause |
|---|---|
first doesn't like [] as input |
Something asked for the first item of an empty list. That is what emptyp guards against. |
| It prints the list all on one line | You have used pr :list rather than pr first :list. |
| It prints brackets you did not expect | show keeps them, pr removes them. |
| It never stops | The list is not getting shorter. Check the recursive call passes bf :list, not :list. |
A list holds several things. first and last take one item out; bf and bl
give back the rest. emptyp says whether there is anything left, and count
says how many. A procedure that calls itself with bf of its input works
through a list of any length.
S16, building lists - putting things into a list rather than taking them out.