Author Topic: Subtract Lists but get remainder?  (Read 5911 times)

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Subtract Lists but get remainder?
« 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?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Subtract Lists but get remainder?
« Reply #1 on: May 11, 2005, 09:01:48 AM »
Code: [Select]
(mapcar
   '(lambda (a b) b)
    list1
    list2
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Subtract Lists but get remainder?
« Reply #2 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)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Subtract Lists but get remainder?
« Reply #3 on: May 11, 2005, 09:06:53 AM »
ohhh, Michael and I read the question differently .. Is there an arbitrator in the room ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Subtract Lists but get remainder?
« Reply #4 on: May 11, 2005, 09:11:33 AM »
I probably took Alan too literal, so I'm guessing I'm wrong.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Subtract Lists but get remainder?
« Reply #5 on: May 11, 2005, 09:16:27 AM »
Ahhhhhh, no.

... perhaps the question was Wrong.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Subtract Lists but get remainder?
« Reply #6 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..
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Subtract Lists but get remainder?
« Reply #7 on: May 11, 2005, 09:28:01 AM »
Ahhh, an intersection of sets; okay.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Subtract Lists but get remainder?
« Reply #8 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:
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Subtract Lists but get remainder?
« Reply #9 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
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Subtract Lists but get remainder?
« Reply #10 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
)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

whdjr

  • Guest
Subtract Lists but get remainder?
« Reply #11 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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Subtract Lists but get remainder?
« Reply #12 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:
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Subtract Lists but get remainder?
« Reply #13 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?

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Subtract Lists but get remainder?
« Reply #14 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 ..
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.