3.3.  Lists, Lists And More Lists

We've trained you in variables and functions, and now enter the murky swamps of Scheme's lists.

Before we talk more about lists, it is necessary that you know the difference between atomic values and lists.

You've already seen atomic values when we initialized variables in the previous lesson. An atomic value is a single value. So, for example, we can assign the variable "x" the single value of 8 in the following statement:

(let* ( (x 8) ) x)

(We added the expression x at the end to print out the value assigned to x-- normally you won't need to do this. Notice how <code>let*</code> operates just like a function: The value of the last statement is the value returned.)

A variable may also refer to a list of values, rather than a single value. To assign the variable x the list of values 1, 3, 5, we'd type:

(let* ( (x '(1 3 5))) x)

Try typing both statements into the Script-Fu Console and notice how it replies. When you type the first statement in, it simply replies with the result:

8

However, when you type in the other statement, it replies with the following result:

(1 3 5)

When it replies with the value 8 it is informing you that x contains the atomic value 8. However, when it replies with (1 3 5), it is then informing you that x contains not a single value, but a list of values. Notice that there are no commas in our declaration or assignment of the list, nor in the printed result.

The syntax to define a list is:

'(a b c)

where a, b, and c are literals. We use the apostrophe (') to indicate that what follows in the parentheses is a list of literal values, rather than a function or expression.

An empty list can be defined as such:

'()

or simply:

()

Lists can contain atomic values, as well as other lists:

(let*
   (
        (x
           '("GIMP" (1 2 3) ("is" ("great" () ) ) )
        )
    )
    x
)
      

Notice that after the first apostrophe, you no longer need to use an apostrophe when defining the inner lists. Go ahead and copy the statement into the Script-Fu Console and see what it returns.

You should notice that the result returned is not a list of single, atomic values; rather, it is a list of a literal <code>("The GIMP")</code>, the list <code>(1 2 3)</code>, etc.