30B. Conceptual Lists

Conceptual lists and a notation for writing lists

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 [ ].

Conceptual list notation only a way of talking about the List ADT. It is not C++ notation.

Operations on conceptual lists

We will use the following notation for working with conceptual lists. This is the interface of the List ADT.

head(L)

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.


tail(L)

The tail of a list L is the list that you get by removing the first member of L. Here are some examples.
  • tail([2, 4, 6, 8, 10]) = [4, 6, 8, 10]

  • tail([5, 4, 3, 2]) = [4, 3, 2]

  • tail([3, 6]) = [6]

  • tail([2]) = [ ]

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.
2 : [4, 6, 8] = [2, 4, 6, 8]
7 : [1, 2] = [7, 1, 2]
8 : [4] = [8, 4]
5 : [ ] = [5]

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

2 : 4 : 6 : [ ] = 2 : 4 : [6]
= 2 : [4, 6]
= [2, 4, 6]

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.)

Exercises

  1. What is head([3, 2, 1])? Answer

  2. What is tail([3, 2, 1])? Answer

  3. What is 9 : [4, 7, 1]? Answer

  4. What is 2 : []? Answer

  5. What is tail([2])? Answer

  6. Does tail([ ]) have a value? If so, what is it? Answer

  7. Does [2, 4, 6] : 8 have a value? If so, what is it? Answer