| First Lisp Assignment: Finger exercises Dave Matuszek |
Assume:
A represents some Atom,
L, L1, L2 represent arbitrary Lists,
LAT represents some List containing zero or more
AToms (and only atoms), and
NEL represents some NonEmpty List.
Do not do any error checking in your functions! Assume that the parameters to your functions are of the correct type. (Checking the types of parameters would add too much complexity to your program.)
(MY-LAST NEL)
NEL.(MY-REMBER A LAT)
LAT with all occurrences of the atom
A removed; remaining elements are in the same order as those
in LAT. Hint: use CAR and CDR to tear
the list apart, and CONS to build the new list.(MY-APPEND L1 L2)
L1, in their
original order, followed by all the elements of L2, in their
original order. For example, (APPEND '(A B C) '(X Y Z)) should
give (A B C X Y Z).(MY-REVERSE L)
(A B (C D))
becomes the list ((C D) B A). Hint:
you probably need MY-APPEND and LIST.(MY-MAKESET LAT)
LAT. For example, given
(A B C A D A B), MY-MAKESET
returns (A B C D). (It is OK for your version
of MY-MAKESET to return the list in a different order, so long
as the correct atoms are present.)(MY-ATOMSOF L)
L with all sublists removed, leaving
only the top-level atoms. For example, if L is (P (Q R) ( ) (( )) S),
the result should be (P NIL S).(MY-SKELETON L)
L, but retains all parentheses.
If L is (P (Q (R S)) T U (V) ( )),
the result is ((( )) ( ) ( )), or in
other words, ((NIL) NIL NIL). Note that in this problem
NIL must be treated as a list, not as an atom.(MY-REVERSEALL L)
L at all levels. For example, if L
is (A (B C (D)) E), MY-REVERSEALL
should return (E ((D) C B) A).(MY-REMBERALL A L)
L with all occurrences of
the atom A removed. This differs from MY-REMBER
in that L may be an arbitrary list, and the atoms A
may occur at any level within L.(MY-COLLAPSE L)
L with all inner parentheses removed. For
example, given (A (B C (D) ( ) E)),
MY-COLLAPSE returns (A B C D E).
The order of atoms must be preserved. These functions are roughly in order of increasing difficulty (MY-REVERSE
may be a little more difficult than some that follow it). Once you have written
a function, use it in other functions as appropriate--if you don't, some functions
will be very difficult!
Note: Some of these functions (such as REVERSE) are already
defined in Lisp; I have added the prefix MY- in order to avoid
name conflicts. Please do not use any of those Lisp functions in this assignment.
Note: Lisp always prints the empty list as NIL rather than
as ( ), and there is no easy way to change this. So in some
of the above functions (especially MY-SKELETON) you will have to
make the translation mentally.