Author Topic: Check 2 lists for exact same elements but not necessarily same order  (Read 5501 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10659
Re: Check 2 lists for exact same elements but not necessarily same order
« Reply #15 on: December 28, 2017, 03:13:30 PM »
Where I learned recursion is "fairly good" (the book that taught many people to program).

https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Check 2 lists for exact same elements but not necessarily same order
« Reply #16 on: December 28, 2017, 06:36:39 PM »
Nice algorithm gile  :-)

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2149
  • class keyThumper<T>:ILazy<T>
Re: Check 2 lists for exact same elements but not necessarily same order
« Reply #17 on: December 28, 2017, 06:46:57 PM »
Ben,
Care to share the circumstances under which you could use this logic. ?

I've had too much trifle during the holiday and I'm partly brain dead.

Regards,
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Ben Clark

  • Newt
  • Posts: 94
Re: Check 2 lists for exact same elements but not necessarily same order
« Reply #18 on: December 28, 2017, 07:17:10 PM »
Ben,
Care to share the circumstances under which you could use this logic. ?

I've had too much trifle during the holiday and I'm partly brain dead.

Regards,



I've got a rather large (by my standards) application I'm writing that saves a bunch of data using vlax-ldata. Because the entries in vlax-ldata dictionaries are not ordered, i have another dictionary with a single entry (a list) that saves an intended order for all the entries in the main dictionary where the data is. So the only point of this second dictionary entry was to store an order that the user can manipulate.

Because I have so many functions doing various things. I wanted to run a check upon starting up the first dialog that ensured the entries in the main data dictionary matched up with the entries in this "auxiliary"  ordered list dictionary entry.

Therefore, I needed a function that would check if they had all the same elements but allow for them to be out of order.
« Last Edit: December 28, 2017, 07:27:46 PM by Ben Clark »

Ben Clark

  • Newt
  • Posts: 94
Re: Check 2 lists for exact same elements but not necessarily same order
« Reply #19 on: December 28, 2017, 10:49:11 PM »


"Well now this problem is 'sorted-out'..."


I just can't sit back and watch this amazing pun fall through the cracks without anyone acknowledging it. It's not the way I function.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Check 2 lists for exact same elements but not necessarily same order
« Reply #20 on: December 29, 2017, 05:24:49 AM »
This looks pretty nice. Although I don't understand recursion yet (somewhat new to FP). Anyone know of a ELI5-ish tutorial for recursion? Has Lee written anything? lol

Code - Auto/Visual Lisp: [Select]
  1. (defun simlists (l1 l2)
  2.   (and
  3.     (= (length l1) (length l2))
  4.     (or
  5.       (equal l1 l2)
  6.       (simlists (vl-remove (car l1) l1) (vl-remove (car l1) l2))
  7.     )
  8.   )
  9. )

This code is a quite clear example of tail recursion using.
If first checks for list lengths equality:
  If lengths aren't equal: return nil (end).
  If length are equal, it checks for list equality:
    If lists are equal: return T (end).
    If lists are not equal: recursive call with both lists without all occurences of the first element of one list.

Here's an 'imperative' implementation:
Code - Auto/Visual Lisp: [Select]
  1. (defun simlists (l1 l2 / val)
  2.   (while
  3.     (and
  4.       (= (length l1) (length l2))
  5.       (not (setq val (equal l1 l2)))
  6.     )
  7.     (setq l2 (vl-remove (car l1) l2)
  8.           l1 (vl-remove (car l1) l1)
  9.     )
  10.   )
  11.   val
  12. )
Speaking English as a French Frog

fools

  • Newt
  • Posts: 72
  • China
Re: Check 2 lists for exact same elements but not necessarily same order
« Reply #21 on: December 29, 2017, 08:24:40 AM »

I meant to communicate that both lists should have the same length. Basically I want to test for the lists having exactly the same elements, and by that I mean if there are duplicate elements, that each list would have the same quantity of said duplicates.

Stated differently: Start with two identical lists and relax only the constraint of element order. Lists that meet this quality are the only ones that should return a non nil value.


Function vl-remove or vl-remove-if will delete duplicate parts, maybe not meet the requirements of BEN.
(simlists '("a" "a" "b") '("b" "b" "a")) return nil not T, is it?
Good good study , day day up . Sorry about my Chinglish .

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Check 2 lists for exact same elements but not necessarily same order
« Reply #22 on: December 29, 2017, 08:46:46 AM »
Function vl-remove or vl-remove-if will delete duplicate parts, maybe not meet the requirements of BEN.
(simlists '("a" "a" "b") '("b" "b" "a")) return nil not T, is it?

This is precisely the behavior you describe which is use in the upper functions.

If you trace the upper recursive simlists function:

(simlists '("a" "a" "b") '("b" "b" "a"))
=> (simlists '("b") '("b" "b"))    ; recursive call
=> nil                                       ; expected result, because list lengths are not equal
Speaking English as a French Frog

fools

  • Newt
  • Posts: 72
  • China
Re: Check 2 lists for exact same elements but not necessarily same order
« Reply #23 on: December 29, 2017, 09:00:09 AM »
(simlists '("a" "a" "b") '("b" "b" "a"))
=> (simlists '("b") '("b" "b"))    ; recursive call
=> nil                                       ; expected result, because list lengths are not equal

I see. I have learned another trick, thanks. :laugh:
Good good study , day day up . Sorry about my Chinglish .

Ben Clark

  • Newt
  • Posts: 94
Re: Check 2 lists for exact same elements but not necessarily same order
« Reply #24 on: December 29, 2017, 10:49:09 AM »
This looks pretty nice. Although I don't understand recursion yet (somewhat new to FP). Anyone know of a ELI5-ish tutorial for recursion? Has Lee written anything? lol

Code - Auto/Visual Lisp: [Select]
  1. (defun simlists (l1 l2)
  2.   (and
  3.     (= (length l1) (length l2))
  4.     (or
  5.       (equal l1 l2)
  6.       (simlists (vl-remove (car l1) l1) (vl-remove (car l1) l2))
  7.     )
  8.   )
  9. )

This code is a quite clear example of tail recursion using.
If first checks for list lengths equality:
  If lengths aren't equal: return nil (end).
  If length are equal, it checks for list equality:
    If lists are equal: return T (end).
    If lists are not equal: recursive call with both lists without all occurences of the first element of one list.

Here's an 'imperative' implementation:
Code - Auto/Visual Lisp: [Select]
  1. (defun simlists (l1 l2 / val)
  2.   (while
  3.     (and
  4.       (= (length l1) (length l2))
  5.       (not (setq val (equal l1 l2)))
  6.     )
  7.     (setq l2 (vl-remove (car l1) l2)
  8.           l1 (vl-remove (car l1) l1)
  9.     )
  10.   )
  11.   val
  12. )



Thanks so much for your clear explanation. I did some reading up on recursion and it makes total sense now. The joys of functional programming...