TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: CAB on May 11, 2005, 08:56:40 AM

Title: Subtract Lists but get remainder?
Post by: CAB on May 11, 2005, 08:56:40 AM
I'm having a mental block.
Here is the code to subtract lists

Code: [Select]
(setq list1 '(1 2 3 4 5)
      list2 '(2 3 4)
)

(vl-remove-if '(lambda (x) (member x list2)) list1)

returns (1 5)


What if I want it to return (2 3 4) the matching items?
Title: Subtract Lists but get remainder?
Post by: MP on May 11, 2005, 09:01:48 AM
Code: [Select]
(mapcar
   '(lambda (a b) b)
    list1
    list2
)
Title: Subtract Lists but get remainder?
Post by: Kerry on May 11, 2005, 09:03:01 AM
Perhaps :
Code: [Select]
(setq list1 '(9 1 4 5 6)
      list2 '(2 3 4)
)

(vl-remove-if-not '(lambda (x) (member x list2)) list1)


;; ==> (4)
Title: Subtract Lists but get remainder?
Post by: Kerry on May 11, 2005, 09:06:53 AM
ohhh, Michael and I read the question differently .. Is there an arbitrator in the room ??
Title: Subtract Lists but get remainder?
Post by: MP on May 11, 2005, 09:11:33 AM
I probably took Alan too literal, so I'm guessing I'm wrong.
Title: Subtract Lists but get remainder?
Post by: Kerry on May 11, 2005, 09:16:27 AM
Ahhhhhh, no.

... perhaps the question was Wrong.
Title: Subtract Lists but get remainder?
Post by: CAB on May 11, 2005, 09:16:29 AM
Kerry's code got the result i was looking for.
Could have sworn I tried that combo with the wrong results.
Go figure. :)

Thanks all..
Title: Subtract Lists but get remainder?
Post by: MP on May 11, 2005, 09:28:01 AM
Ahhh, an intersection of sets; okay.
Title: Subtract Lists but get remainder?
Post by: CAB on May 11, 2005, 09:28:56 AM
Well now this works too...
Not sure how it works though?

Code: [Select]

_$ (setq list1 '("a" "b" "c" "d"))
("a" "b" "c" "d")
_$ (setq list2 '("b" "c"))
("b" "c")
_$ (mapcar
   '(lambda (a b) b)
    list1
    list2
)
("b" "c")
_$


I'll be back this afternoon,  and try to understand this one. :roll:
Title: Subtract Lists but get remainder?
Post by: MP on May 11, 2005, 09:31:20 AM
It's an illusion Alan. Reverse the order of list1 and 2 and see what happens:

Code: [Select]
(mapcar
   '(lambda (a b) b)
    list2
    list1
)
Title: Subtract Lists but get remainder?
Post by: Kerry on May 11, 2005, 09:33:54 AM
Cab, This may help sort it out :)
Code: [Select]

(setq list1 '(nil 1 ))

(setq list2 '("d" "c"))

(mapcar
   '(lambda (a b) b)
    list1
    list2
)
Title: Subtract Lists but get remainder?
Post by: whdjr on May 11, 2005, 09:47:14 AM
I took the code you guys provided and elaborated a little bit:
Code: [Select]
(setq lst1 '(1 2 3 4 5))
(setq lst2 '(5 9 3 2 0 8))

(defun Return_Unique_Items (lst1 lst2)
  (vl-remove-if '(lambda (x) (member x lst2)) lst1)
)
;Returns: (1 4)

(defun Return_Same_Items (lst1 lst2)
  (vl-remove-if-not '(lambda (x) (member x lst2)) lst1)
)
;Returns: (2 3 5)

(defun Return_Unique_Items2 (lst1 lst2)
  (mapcar 'Return_Unique_Items (list lst1 lst2) (list lst2 lst1))
)
;Returns: ((1 4) (9 0 8))

The last function returns the items that are unique in their respective lists.
Title: Subtract Lists but get remainder?
Post by: Kerry on May 11, 2005, 10:09:22 AM
logically, when dealing with boolean operations, the operations are :

UNION
Returns the result of combining the primary and secondary Sets.

COMBINE
Returns Similar to the Union operation but removes the Members in common.

SUBTRACT
Returns a set of Members with common Members removed from the Primary Set

INTERSECT
Returns a set of everything <edit> common to the Sets



Using these terms should not cause a problem.

editted:
I got NOTted.  :oops:
Title: Subtract Lists but get remainder?
Post by: MP on May 11, 2005, 10:43:15 AM
Begin the colonel's pardon, but as I recall set theory (admittedly, ions ago) ...

INTERSECT
Returns a set of everything common to the Sets. That is, given sets A and B, the resultant set contains only those items that exist in both A and B.

Am I wrong?

:)
Title: Subtract Lists but get remainder?
Post by: Kerry on May 11, 2005, 10:54:15 AM
Oooops a typo. The NOT will be subtracted.

You are NOT wrong. Good pickup.

bed time I think ..
Title: Subtract Lists but get remainder?
Post by: MP on May 11, 2005, 11:05:09 AM
We take turns my friend, it was merely mine this time. Night, night, see ya tomorrow.

:)