TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: CAB on May 11, 2005, 08:56:40 AM
-
I'm having a mental block.
Here is the code to subtract lists
(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?
-
(mapcar
'(lambda (a b) b)
list1
list2
)
-
Perhaps :
(setq list1 '(9 1 4 5 6)
list2 '(2 3 4)
)
(vl-remove-if-not '(lambda (x) (member x list2)) list1)
;; ==> (4)
-
ohhh, Michael and I read the question differently .. Is there an arbitrator in the room ??
-
I probably took Alan too literal, so I'm guessing I'm wrong.
-
Ahhhhhh, no.
... perhaps the question was Wrong.
-
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..
-
Ahhh, an intersection of sets; okay.
-
Well now this works too...
Not sure how it works though?
_$ (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:
-
It's an illusion Alan. Reverse the order of list1 and 2 and see what happens:
(mapcar
'(lambda (a b) b)
list2
list1
)
-
Cab, This may help sort it out :)
(setq list1 '(nil 1 ))
(setq list2 '("d" "c"))
(mapcar
'(lambda (a b) b)
list1
list2
)
-
I took the code you guys provided and elaborated a little bit:
(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.
-
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:
-
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?
:)
-
Oooops a typo. The NOT will be subtracted.
You are NOT wrong. Good pickup.
bed time I think ..
-
We take turns my friend, it was merely mine this time. Night, night, see ya tomorrow.
:)