List

Lists in LOGO are used to store data collections and as executable code blocks.

Collection. Elements of a list can have any type; different data types can be mixed in a single list. Declaration of a constant list is following: [a b c 1 2] (elements are separated with white spaces; all elements are recognized as words; brackets [] and {} used inside the list are recognized as nested lists and arrays).
Size of a list can be modified by adding or removing elements from the beginning or the end of the list (see instructions: queue, dequeue, push, pop). Queues and stacks can be implemented in this way. The prefered method of sequential access to elements is foreach loop. Outermost elements can be accessed with instructions first and last. Instruction item and indexing operator (,) allow to read elements at any position, however they are expensive (array is prefered for efficient random access to elements).

Elements in lists are immutable. Elements cannot be modified e.g. with setitem) instruction.

Example 1:

make "lis [a b [c]]
print :lis
queue :lis 123
print :lis
print dequeue :lis
print :lis
print :lis,2
print :lis,2,1

Output:

[a b [c]]
[a b [c] 123]
a
[b [c] 123]
[c]
c

Source code block. Many instructions in LOGO use lists of instructions as arguments e.g. loops and conditional statements. If such a list is provided as a constant in the code, then its content is compiled together with the whole program.

Example 2:

make "x 0
repeat 5 [make "x :x + repcount print :x]

Output (sum of 1, ..., iteration number is calculated on each iteration):

1
3
6
10
15

Code block can be created also dynamically, during the program execution. In such case code will be compiled before it is executed for the first time. If the list with code block is modified then the code is recompiled accordingly.

Example 3:

make "fn [sqrt exp log]
foreach "n :fn (list "print :n 2)

Output:

1.4142135623731
7.38905609893065
0.693147180559945

Consecutive iterations of the foreach loop in the above example are performing following operations:

print sqrt 2
print exp 2
print log 2

See also:

List constructors:
list, sentence
Conversions:
listtoarray, arraytolist
Operations on collections:
queue, dequeue, push, pop
first, butfirst, last, butlast
count, emptyp, listp

Table of Content