next up previous
Contents Next: Defining Lisp functions Up: LISt Processing Previous: Setf

Exercises

Note: A temptation if you are not used to computers is to sit down and try to work out these exercises in your head and be satisfied when you have reached some answer or other. DON'T! Use the Lisp interpreter to check your understanding. If you don't understand why the interpreter responds in some way, try to figure it out by playing with some slight variations of the problems. Some of the examples below (intentionally) generate interpreter errors. In such cases, think about why the error occurs and how you might change things to eliminate it.

1.
Evaluate the following:
a.
(first '(((a)) (b c d e)))
b.
(rest '(((((f))))))
c.
(first '(rest (a b c)))
d.
(first '(rest (rest (a b c))))
e.
(cons '(my life as) '(a dog))
f.
(append '(my life as) '(a dog))
g.
(list '(my life as) '(a dog))
h.
(cons (rest nil) (first nil))
i.
(abs (- (length (rest '(((a b) (c d))))) 5))
j.
(reverse (cons '(rest (reverse '(its gut na mur ta give captin)))))

2.
Using first and rest extract the atom ``jim'' from the following:
a.
(he is dead jim)
b.
(captain (((jim) kirk)))
c.
(((((spock) asked) jim) if) he was all right)
d.
(after (looking at the (lizard man) ((((jim))) asked for warp 9)))

3.
What is returned by each of the following expressions (assume they are evaluated in the given order)?
a.
(setf trek '(picard riker laforge worf))
b.
(cons 'data trek)
c.
trek
d.
(length (cons 'troi trek))
e.
(setf trek (cons 'data trek))
f.
(length (cons 'troi trek))

4.
Given the following definition:
(setf mylist '((bush broccoli) (nixon watergate)
              (letterman (viewer mail))
              (you are no jack kennedy)
              (and please) (scorsese (robert deniro))))
Construct the following with any of the functions you have learned so far.
a.
(no broccoli please)
b.
((scorsese and deniro) are no robert kennedy)
c.
(watergate and no viewer)
d.
(bush nixon kennedy)
e.
((bush broccoli) (nixon watergate) (letterman mail))



© Colin Allen & Maneesh Dhagat
March 2007