A list of integers is a sequence of zero or more integers. We write lists in square brackets, such as [2, 4, 6]. The order matters; the first member of [2, 4, 6] is 2, the second member is 4 and the third member is 6. There can be repetitions. For example, [1, 1, 1, 1] is a list of four integers.
List [3] is a list with one integer; it is not the same as the number 3 because it does not have the same type; 3 is a number but [3] is a list. The empty list is written [ ].
We will use the following notation for working with conceptual lists. These are the operations of the ADT. Notice that all of this is at the conceptual level; it is not C++ notation.
The head of a list is the first member of the list.
For example, head([2, 4, 6, 8]) = 2 and head([5]) = 5.
Notice that the parameter of head must be a list, and
its result is an integer.
The empty list does not have a head, and computing head([ ]) is an error. |
The tail of a list L is the list that you get
by removing the first member of L. Here are some examples.
The tail of a list is always a list. The empty list has no tail, and computing tail([ ]) is an error. |
h:t
Expression h:t is the list
whose head is h and
whose tail is t. Here are some examples.
Notice that the left-hand operand of : is always an integer and the right-hand operand is always a list. Expression h:t always yields a list, namely the list that you get by adding h to the beginning of list t. Don't expect it to do anything else. By convention, operator : is right associative. That is, it is done from right to left. So
|
isEmpty(L)
Expression isEmpty(L) is true if L is an empty list. |
emptyList
Constant emptyList is an empty list. (We write [ ] for it, but it is important to realize that an empty list is a key component of the abstract data type.) |
What is head([3, 2, 1])? Answer
What is tail([3, 2, 1])? Answer
What is 9:[4, 7, 1]? Answer
What is 2:[]? Answer
What is tail([2])? Answer
Does tail([ ]) have a value? If so, what is it? Answer
Does [2, 4, 6]:8 have a value? If so, what is it? Answer