From: ···········@gmail.com
Subject: Appending distinct elements at the end of the list
Date: 
Message-ID: <b3247bb7-1e0e-46e5-aec3-dd4339e20691@b1g2000hsg.googlegroups.com>
I have to remove the repeated elements from list2 and append them at
the end of list1, and return the modified list1 afterwards.

Here's a function that I wrote but it is not working.
(defun push-distinct (list1 list2)
	(let ((temp list2))
		(dolist (x list1)
			(setf temp
				(remove x temp :test #'equal)))
		(dolist (x temp)
			(setf list1
				(push list1 x)))))

What am I doing wrong here?

From: Kenny Tilton
Subject: Re: Appending distinct elements at the end of the list
Date: 
Message-ID: <TN2dnUf9Nc8M1WPanZ2dnUVZ_t_inZ2d@giganews.com>
···········@gmail.com wrote:
> I have to remove the repeated elements from list2 and append them at
> the end of list1, and return the modified list1 afterwards.
> 
> Here's a function that I wrote but it is not working.
> (defun push-distinct (list1 list2)
> 	(let ((temp list2))
> 		(dolist (x list1)
> 			(setf temp
> 				(remove x temp :test #'equal)))
> 		(dolist (x temp)
> 			(setf list1
> 				(push list1 x)))))
> 
> What am I doing wrong here?

not returning anything. (dolist (x y y) ...) will return y.

hth,k
From: Ken Tilton
Subject: Re: Appending distinct elements at the end of the list
Date: 
Message-ID: <47fe5b89$0$5604$607ed4bc@cv.net>
Kenny Tilton wrote:
> ···········@gmail.com wrote:
> 
>> I have to remove the repeated elements from list2 and append them at
>> the end of list1, and return the modified list1 afterwards.
>>
>> Here's a function that I wrote but it is not working.
>> (defun push-distinct (list1 list2)
>>     (let ((temp list2))
>>         (dolist (x list1)
>>             (setf temp
>>                 (remove x temp :test #'equal)))
>>         (dolist (x temp)
>>             (setf list1
>>                 (push list1 x)))))
>>
>> What am I doing wrong here?
> 
> 
> not returning anything. (dolist (x y y) ...) will return y.

Sorry, I was in a hurry. Since you have two lists to return, of course 
that last dolist might be:

   (dolist (x temp (list temp list1))...)

Or you could just follow the last dolist with a new form:

   (list temp list1)

kt


-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/

"I've never read the rulebook. My job is to catch the ball."
   -- Catcher Josh Bard after making a great catch on a foul ball
and then sliding into the dugout, which by the rules allowed the
runners to advance one base costing his pitcher a possible shutout
because there was a runner on third base.

"My sig is longer than most of my articles."
   -- Kenny Tilton
From: John Thingstad
Subject: Re: Appending distinct elements at the end of the list
Date: 
Message-ID: <op.t9em5pg6ut4oq5@pandora.alfanett.no>
P� Thu, 10 Apr 2008 18:17:43 +0200, skrev <···········@gmail.com>:

> I have to remove the repeated elements from list2 and append them at
> the end of list1, and return the modified list1 afterwards.
>
> Here's a function that I wrote but it is not working.
> (defun push-distinct (list1 list2)
> 	(let ((temp list2))
> 		(dolist (x list1)
> 			(setf temp
> 				(remove x temp :test #'equal)))
> 		(dolist (x temp)
> 			(setf list1
> 				(push list1 x)))))
>
> What am I doing wrong here?

It returns nil right? Why don't you try to return list1. dolist has a  
optional third argument which tells it what to return otherwise it returns  
nil. Besides you are appending it to the front in reverse order.
(push item place) so the second pushes list1 onto x.

Besides that (let ((list (reverse list1))) (nreverse (dolist (x list2  
list) (pushnew x list))) is more succinct.

or just (remove-duplicates (append list1 list2)) but that would remove  
duplicates in list1 too.

--------------
John Thingstad
From: John Thingstad
Subject: Re: Appending distinct elements at the end of the list
Date: 
Message-ID: <op.t9esl60nut4oq5@pandora.alfanett.no>
P� Thu, 10 Apr 2008 19:06:51 +0200, skrev John Thingstad  
<·······@online.no>:

> P� Thu, 10 Apr 2008 18:17:43 +0200, skrev <···········@gmail.com>:
>
>> I have to remove the repeated elements from list2 and append them at
>> the end of list1, and return the modified list1 afterwards.
>>
>> Here's a function that I wrote but it is not working.
>> (defun push-distinct (list1 list2)
>> 	(let ((temp list2))
>> 		(dolist (x list1)
>> 			(setf temp
>> 				(remove x temp :test #'equal)))
>> 		(dolist (x temp)
>> 			(setf list1
>> 				(push list1 x)))))
>>
>> What am I doing wrong here?
>
> It returns nil right? Why don't you try to return list1. dolist has a  
> optional third argument which tells it what to return otherwise it  
> returns nil. Besides you are appending it to the front in reverse order.
> (push item place) so the second pushes list1 onto x.
>
> Besides that (let ((list (reverse list1))) (nreverse (dolist (x list2  
> list) (pushnew x list))) is more succinct.
>
> or just (remove-duplicates (append list1 list2)) but that would remove  
> duplicates in list1 too.
>

oops.. re-reading it it looks more like
(nconc list1 (remove-duplicates list2)) is what is asked for.

--------------
John Thingstad
From: Thomas A. Russ
Subject: Re: Appending distinct elements at the end of the list
Date: 
Message-ID: <ymimyo0l3s0.fsf@blackcat.isi.edu>
···········@gmail.com writes:

> I have to remove the repeated elements from list2 and append them at
> the end of list1, and return the modified list1 afterwards.
> 
> Here's a function that I wrote but it is not working.
> (defun push-distinct (list1 list2)
> 	(let ((temp list2))
> 		(dolist (x list1)
> 			(setf temp
> 				(remove x temp :test #'equal)))
> 		(dolist (x temp)
> 			(setf list1
> 				(push list1 x)))))
> 
> What am I doing wrong here?

You are not returning any values from your function.

LET returns the value of the last form.
DOLIST returns the optional third element in its first list argument.
In your case, the function will always return NIL.

Not to mention that you are putting the new items on the front of List1
rather than the back....

Now, in this particular case, you can avoid going through the lists
twice by using the FIND test from your previous code and only inserting
when you don't find the answer:

  (defun push-distinct (list1 list2)
    (dolist (x list2)
       (unless (find x list1 :test #'equal)
          (push x list1)))
    list1)


Of, course, there's always

  (intersection list1 list2 :test #'equal)

assuming that LIST1 and LIST2 are proper sets to begin with.  This
doesn't guarantee any order, though.  If the order is really important,
then you would have to do something like:

  (append list1 (set-difference list2 list1 :test #'equal))



-- 
Thomas A. Russ,  USC/Information Sciences Institute