Since this is your first homework and you are new to Lisp, here is a very simple top-down approach without worrying about performance and making good use of the CL tools:
Common Lisp already has a function that removes duplicates: remove-duplicates . Using it with :from-end keyword argument will "keep order". Now imagine that you have a flatten function that aligns arbitrary nested lists. Then the solution to your question will be:
(defun new-union (list1 list2) (remove-duplicates (flatten (list list1 list2)) :from-end t))
This is how I would approach the problem when no additional restrictions are given, and there is no real reason to worry about performance. Use as much of this toolkit as possible and do not reinvent the wheels if necessary.
If you come up with a problem like this, it comes down to writing a flatten function, which I will leave as an exercise for you. This is not too complicated, one simple option is to write a recursive function, approaching such a problem:
If the first element of the list to be flattened is itself a list, add the flattened first element to the flattened remainder. If the first item is not a list, just add it to the flattened rest of the list. If the input is not a list at all, just return it.
This should be a good exercise for you, and it can be done with just a few lines of code.
(If you want to be very faithful, use a helper function to do the work and check the wrapping function to see if the argument is really a list. Otherwise, flatten will work on atoms, which may or may not be a problem for you.)
Now, if you wrote flatten :
> (defun new-union (list1 list2) (remove-duplicates (flatten (list list1 list2)) :from-end t)) NEW-UNION > (new-union 'a 'b) (AB) > (new-union 'a '(b)) (AB) > (new-union '(ab) '(bc)) (ABC) > (new-union '(((a))) '(b (c ((de)) a))) (ABCDE)