CSE 341 -- Programming Languages
Autumn 2001
|
Department of Computer Science and Engineering,
University of Washington
Steve Tanimoto (instructor)
|
Assignment
2
Version 1.0 of October 8 |
Introduction to Lisp
Due date and time: Thursday, October 18,
2001 (at the beginning of section).
Turn in this assignment as a hardcopy printout. |
Title: Lisp Macros.
Purposes:
Gain exposure to Lisp macros, gain fluency writing Lisp macros,
demonstrate mastery of basic Lisp constructs,
see how Lisp macros play an important role in creating
embedded languages within Lisp.
Instructions:
Study the short chapter on writing Lisp macros.
Try performing the MACROEXPAND operation on various
calls to built-in macros. Then do the following problems.
-
1. Write a macro ZERO-IT that sets the value of
its argument (which must be an unquoted symbol) to 0.
For example, we might have:
> (zero-it x)
0
> x
0
-
2. Write a macro ZERO-THEM which takes any number of
unquoted symbols as arguments and sets them all to 0.
For example:
> (zero-them x y z)
0
> x
0
> y
0
> z
0
-
3. Write a macro THRICE-EACH that takes any number of
forms as arguments and evaluates the first one three
times, the second one three times, etc., in that order.
At the end, it returns the result of the last evaluation
of the last form.
> (thrice-each
> (setq x 0)(incf x)(print x)(setq x (* x 2))(print x) )
3
3
3
24
24
24
24
-
4. Now let's take a brief break from writing
macros in order to get familiar with the
manipulation of ordered pairs (which are used
in exercise 5, which involves writing a macro
again.)
Write a function ORDERED-PAIRS that takes
a list of elements and returns a new list
containing all ordered pairs of the elements.
> (ordered-pairs '(a b c))
((a a)(a b)(a c)(b a)(b b)(b c)(c a)(c b)(c c))
-
5. Write a macro FOR-ALL-UNORDERED-PAIRS that
will take a list of two symbols, a list of 1 or
more values, and then any number of forms, and
it will repeatedly execute the sequence of forms,
once for each "unordered" pair of the values,
with the two values bound to the two variables.
After doing that processing, it will return NIL.
For example, the following call would work as shown:
> (for-all-unordered-pairs (x y) '(1 2 3 4)
> (print (cons x y))
> (print (* x y)) )
(1 . 1)
1
(1 . 2)
2
(1 . 3)
3
(1 . 4)
4
(2 . 2)
4
(2 . 3)
6
(2 . 4)
8
(3 . 3)
9
(3 . 4)
12
(4 . 4)
16
NIL
By unordered, we mean that reversing the order of
two values does not lead to another iteration in
the loop. For example, (3 . 4) gets printed in
one of the iterations, but (4 . 3) never gets
printed, because it's considered equivalent to
the (3 . 4).
-
6. Write a macro FOR-ALL-UNORDERED-PAIRS-LIST
that takes similar arguments and behaves
similarly to FOR-ALL-UNORDERED-PAIRS, but
rather than return NIL, it returns a list
of all the values, from each iteration, of
the last form, If it were used in the example
above, it would return
(1 2 3 4 4 6 8 9 12 16)
Individual Work: This assignment
requires individual work. Do NOT work in teams on this assignment..