Beginner's Question on an exercise from Graham's ACL

From: A. Michael Perry (amperry_at_provide.net)
Date: 08/24/04


Date: Tue, 24 Aug 2004 06:57:05 GMT

Hello folks,

I'm trying to get through Graham's ANSI Common Lisp, and I've gotten
myself hung up on an exercise: chapter 3, ex. 2. "Write a version of union
that preserves the order of the elements in the original lists:
>(new-union '(a b c) '(b a d))
(A B C D)"

I've come up with something that gets close, but not quite. (Strangely,
the concept of recursion doesn't seem so hard, but operations on lists
seem to be throwing me.)

(defun unite (ls1 ls2)
        (if (null ls2)
                ls1

        (if (member (car ls2) ls1)
                (unite ls1 (cdr ls2))
                (unite (list ls1 (car ls2)) (cdr ls2)))))

--with this, (unite '(a b c) '(b a d)) generates the following:
((A B C) D)

If I substitute "append" for list in the last line above, I get a list
like so:
(A B C . D)

And substituting cons gets everything in one list, but in the wrong order.

Any ideas on what I'm getting wrong here?



Relevant Pages

  • SQL Injection: Issue with UNION SELECT ALL
    ... using a UNION SELECT ALL statement. ... an equal number of expressions in their target lists. ... Column 'a.id' is invalid in the select list because it is not contained ... in an aggregate function and there is no GROUP BY clause. ...
    (Pen-Test)
  • RE: report that includes multi tables
    ... report by last name and not have to go through each report by year, ... The next question I have is how to do a UNION instead of a JOIN. ... You couldn't join the two lists because ... >> is auto generated. ...
    (microsoft.public.access.gettingstarted)
  • Re: substructure sharing in #union result
    ... REMOVING the references to variables from those lists when they get ... This will be the union of those ... Just put CLOS objects representing variables in your sets and modify ...
    (comp.lang.lisp)
  • Re: substructure sharing in #union result
    ... This will be the union of those ... Just put CLOS objects representing variables in your sets and modify some slot representing a binding in each object. ... So the union of two lists is guaranteed to contain the same objects (under #'eql) as the original lists. ...
    (comp.lang.lisp)
  • Re: Insert symbol between every element in a list (From HtDP book)
    ... I'm just learning Scheme with the book 'How to Design Programs'. ... extended exercise. ... Guideline on Wish Lists ... with the first argument inserted between all letters and at the ...
    (comp.lang.scheme)