Dr Logo for the Tatung Einstein, showing a rosette drawn by the turtle
← Back to Courses
module
16

Building lists

Introduction

S15 took lists apart. Going the other way - building one up - is what lets a procedure answer a question with several things rather than one.

Three ways in

?show fput "red [green blue]
[red green blue]
?show lput "blue [red green]
[red green blue]
?show se [red green] [blue]
[red green blue]

fput - first put - puts one thing on the front. lput - last put - puts one on the end. se - sentence - takes two lists and runs them together into one.

The difference between lput and se is worth being clear about: lput adds an item, se joins two lists. Give lput a list and the whole list goes in as one item:

?show lput [c d] [a b]
[a b [c d]]
?show se [a b] [c d]
[a b c d]

A list inside a list is a perfectly good thing to have, and occasionally what you want. It is rarely what you meant when you reached for lput.

None of them changes what you gave them. They hand back something new, exactly as bf and bl did.

A procedure that builds one

Put fput together with the recursion from S14 and a procedure can build a list as it goes:

to countdown :n
if :n < 1 [op []]
op fput :n countdown :n - 1
end

If there is nothing left to count, hand back an empty list. Otherwise hand back this number, with the rest of the countdown after it.

That is a procedure whose answer is a list, not a number - op will hand back either. Each call adds one item to the front of whatever the calls beneath it produced, so the list assembles itself from the inside out.

< is the other way round from the > in S14: less than. There is also =.

The code

Starting from

A fresh boot, at the ? prompt. The minus sign is SHIFT and the = key - the key marked - gives an underscore and Dr Logo will not know what to do with it.

What you should see

countdown defined
[5 4 3 2 1]

A list built by a procedure that calls itself

The brackets are there because show keeps them. That matters here: it is how you can tell you have been given one list of five things rather than five separate answers.

Change one thing

  • Change fput to lput. The numbers come out in the other order. Work out why before you run it - think about which end each call is adding to.
  • Change show to pr. The same list, printed differently. Which of the two would you use to check that a procedure really did hand back a list?
  • Give countdown a name that is already a primitive - to count :n - and see what happens. Then try show count 5 anyway. What did Dr Logo run instead, and what did it say?

When it goes wrong

Symptom Cause
Out of LOGO stack space The recursion never stops. Usually the input is not getting smaller - check the minus sign is SHIFT and =.
I don't know how to _ The key marked - again.
[... ] didn't output to show The thing you called does not hand a value back. If you meant a procedure of your own, check the name is not already a primitive.
A list inside your list lput adds an item. To join two lists use se.
The order is back to front fput adds to the front, lput to the end.

Summary

fput adds to the front of a list, lput to the end, and se joins two lists into one. A procedure can hand back a list with op, and a recursive one can build a list up as it goes.

Next

S17, remembering things - giving a name to a value so you do not have to pass it along every time.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.