next up previous contents
Next: Arrays Up: Lists and Arrays Previous: Lists and Arrays

Lists

Whereas a set contains distinct unordered items, the Darwin list data type allows the user to create ordered multisets. A multiset is a set which allows more than one instance of the same object. An ordered list is a group of expressions surrounded by square brackets [, ].

 
> FirstTry := [1, 1, 2, 3]; 
FirstTry := [1, 1, 2, 3]
> SecondTry := [1, 2, 3]; 
SecondTry := [1, 2, 3]
The two lists are not equivalent since FirstTry has two copies of the number 1 while SecondTry has only a single copy.
> AscendList := [1, 2, 3, 4];
AscendList := [1, 2, 3, 4]
> DescendList := [4, 3, 2, 1];
DescendList := [4, 3, 2, 1]
Again, AscendList and DescendList are not equal since the order of the elements in the list matters.

Like a set, a list are allowed to be heterogeneous (elements may have different types). We can also include a list inside of a list.

> NestedLists := [1, [2, [3, 4], [5]], []];
NestedLists := [1, [2, [3, 4], [5]], []]
> MixedUp := ['hello', [1, 2, 3], [a, b, c], [[], []]];
MixedUp := [hello, [1, 2, 3], [a, b, c], [[], []]]

We could define a list of animals as follows:

> animals := ['chimpanzee', 'gorilla', 'monkey', 'orangutan'];
animals := [chimpanzee, gorilla, monkey, orangutan]
We can think of the list as a chain with the first element containing the string item 'chimpanzee'. The second element of the chain is 'gorilla' and so forth.


  
Figure: Viewing the list as a chain.
\begin{figure}\centerline{\psfig{file=Diagrams/chain.ps,width=5in}}
\end{figure}

To access the individual elements of animals, we provide a number (or range of numbers) indicating which link(s) in the chain we are interested in.

> animals[1];                 # the first link in the chain
chimpanzee
> animals[2..4];              # the second through fourth links in the
                              #   chain
[gorilla, monkey, orangutan]


next up previous contents
Next: Arrays Up: Lists and Arrays Previous: Lists and Arrays
Gaston Gonnet
1998-09-15