First Lisp Assignment: Finger exercises
Dave Matuszek

Write and test the following Lisp functions. Please turn in a listing of your functions, along with a listing showing your tests of these functions.

Assume:

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)
Returns as value the last S-expression which occurs as a member of the nonempty list NEL.

(MY-REMBER A LAT)
Returns as value the list 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)
Forms one list composed of all the elements of 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)
Reverses the elements of list L. For example, the list (A B (C D)) becomes the list ((C D) B A). Hint: you probably need MY-APPEND and LIST.

(MY-MAKESET LAT)
Removes duplicate atoms occurring in 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)
Returns as value the list 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)
Removes all the non-nil atoms of list 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)
Reverses the elements of 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)
Returns as value the list 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)
Returns the list 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.