30A. Equations about Lists

You are familiar with equations about real numbers from algebra. For example, you know that, for all real numbers x, y and z,

x + (y + z) = (x + y) + z

We can also write equations about lists. Here are a few that are true for all integers n and lists y.

2 : [ ]  =  [2]
n : [ ]  =  [n]
[3, 4, 5]  =  3 : [4, 5]
tail([3, 4, 5])  =  [4, 5]
tail(n : y)  =  y
head([3, 4, 5])  =  3
head(n : y)  =  n

Suppose that length(x) is the length of list x. Here are some equations about length.

length([2, 4, 6])  =  3
length([ ])  =  0
length(n : y)  =  length(y) + 1

Exercises

  1. Say whether each of the following equations is true for all values of the variables that it contains. Try checking the equations for particular values of the variables. (The values might be lists.) If the equation makes no sense, say so.

    For this question, x ++ y is the concatenation of lists x and y, which glues lists x and y together. For example, [2, 4, 6] ++ [3, 7] = [2, 4, 6, 3, 7].

    1. [ ] ++ y = yAnswer

    2. y ++ [ ] = yAnswer

    3. length([n, y]) = 1 + length(y)  Answer

    4. x ++ y = x : yAnswer

    5. (n : x) ++ y = n : (x ++ y)  Answer

    6. x ++ (y ++ z) = (x ++ y) ++ zAnswer

    7. length(x ++ y) = length(x) + length(y)  Answer