Lists, Symbols and Parentheses

This lab provides practice with lists, quotes, symbols, and parentheses in Scheme. In particularly,

List Constructor and Manipulation Procedures

Two basic procedures that construct new list structures are cons and list. If y is a list, then we can think of (cons x y) as adding the new element x to the front of the list y.

   (cons 'a '(b c))    =  (a b c)
   (cons 'a '())       =  (a)
   (cons '(a) '(b))    =  ((a) b)
The list procedure makes a list out of the elements that follow:

   (list 'a 'b 'c 'd)  = (a b c d)
   (list 'a '(b c))    =  (a (b c))
   (list 'a '())       = (a ())
   (list '(a) '(b))    =  ((a) (b))
The append procedure makes a new list consisting of the members of its argument lists. append takes any number of arguments.

   (append '(a) '(b))       =  (a b)
   (append '(a b) '(c d))   =  (a b c d)
   (append '(a) '(b) '(c))  =  (a b c)

Exercise 1

Evaluate the following expressions and explain why each result is obtained.


(append '(a b c) '( ))
(list '(a b c) '( ))
(cons '(a b c) '( ))
(cons 'a '(b c d))
(list 'a '(b c d))
(cons '(a b c) 'd)
(list '(a b c) 'd)

One of these expressions yields a result containing a dot? Be sure you can explain why.

Exercise 2

  1. For each of the following, either construct the given list using the list operation, or explain why the expression cannot be constructed using list. That is, try to produce the given expression by filling in the dots in the statement (list ...)

    
       (a b)
       ()
       ((a) b (c))
    
  2. Repeat part a, using the cons operation.

  3. Repeat part a, using the append operation.

Exercise 3

  1. Construct the following lists from just the null list, the symbols a and b, and the cons operation.

    
       (a)
       (a b)
       ((a))
    
  2. Repeat part a, using the list operation instead of cons.

Exercise 4

Explain the result of the evaluation of the following:


   (append 'a '(b))
   (append '(a) 'b)
   (append '(a) '(b))
   (append '((a)) '((b)))

If the expression generates an error, explain why the error arises.

List Extraction Functions

Parts of lists can be extracted by car and cdr:

Examples:


   (car '(a b c))          =  a
   (car '((a) b c))        =  (a)
   (car (car '((a) b c)))  =  a
   (car 'a)     error: a is not a list.
   (car '())    error: () is not a pair.

   (cdr '(a b c))          =  (b c)
   (cdr '((a) b c))        =  (b c)
   (cdr (cdr '(a b c)))    =  (c)
   (cdr (car '((a) b c)))  =  ()
   (cdr 'a)     error: a is not a list.
   (cdr '())    error: () is not a pair.
car and cdr are applied to a list which is introduced with a quote. Since parentheses are used both to group data into a list and to call procedures, the Scheme interactive interface needs to see a single quote at the front of a list literal, so that it will treat it as a datum instead of evaluating it.

Since combinations of car and cdr are frequently used, all combinations up to four uses of car and cdr are defined as functions of the form cxxxr:


   (caar x)    = (car (car x))
   (cadr x)    = (car (cdr x))
   (caddr x)   = (car (cdr (cdr x)))

Exercise 5

use combinations of car and cdr as needed to extract the following from the list (a b (c)).


    b
    (c)
    c

Exercise 6

(From a lab on lists by John Stone.)

Consider the expression (all x (if (human x) (mortal x))). Write lisp expressions to extract each of the following:


   all
   if
   human

Exercise 7

What is the cdr of a one-element list?

The Quote Procedure

The procedure quote returns whatever follows without evaluations. Since this procedure is used often, Scheme provides a simple abbreviation:

(quote expression)

may be abbreviated

'expression

Exercise 8

For this and the remaining exercises (through Exercise 13), begin with these definitions:


   (define p +)
   (define r 5)
   (define s 7)

Evaluate the following expressions and the explain the result of each.


   r
   (quote r)
   s
   (quote s)
   (p r s)
   (quote (p r s))

Exercise 9

Check that the following sequence yields exactly the same results as the follows


   r
   'r
   s
   's
   (p r s)
   '(p r s)

In each case, be sure you can explain the result obtained.

Exercise 10

The symbol? procedure returns true #t when applied to a symbol and false #f otherwise. The number? likewise returns true if it is applied to numeric quantities.

Evaluate the following expressions and explain the results.


   (symbol? r)
   (number? r)
   (symbol? 'r)
   (number? 'r)
   (symbol? ''r)
   (number? ''r)

Parentheses

Parentheses are used to place items onto a list data structure. Lists may be evaluated or not:

Exercise 11

Each of the following two expressions are valid, but they produce rather different results. Explain why each result is obtained.


   (list p r s)
   (list 'p 'r 's)
   (list (p r s))
   (list '(p r s))
   (list '('p 'r 's))

Exercise 12

Try to evaluate each of the following expressions. In each case, describe why the value is obtained or explain why an error results.


   + 2 3
   (+ 2 3)
   '(+ 2 3)
   (+ (2) (3))
   ((+) 2 3)

Exercise 13

The quadratic equation 9x2 + 24x + 16 = 0 has exactly one solution. Use Scheme to find it. (Reminder: The ``quadratic formula'' says that the solutions to the quadratic equation ax2 + bx + c = 0 are (-b + (b2 - 4ac)1/2)/(2a) and (-b - (b2 - 4ac)1/2)/(2a).)

Examine your parentheses carefully, and explain why the parentheses are placed where they are.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~walker/courses/151.fa05/labs/lists-et-al.xhtml

created 12 September 2005
last revised 13 September 2005
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.