Author Topic: Find identical list sections - compare lists?  (Read 2429 times)

0 Members and 1 Guest are viewing this topic.

Peter2

  • Swamp Rat
  • Posts: 650
Find identical list sections - compare lists?
« on: April 09, 2014, 12:42:03 PM »
Hi

are there known subroutines to find identical sections (pairs?) in lists? For example I have

Code: [Select]
(setq main (list "a" "b" "c" "d" "e"))
(setq b (list "a" "c" "e"))
(setq c (list "a" "b" "e"))
(setq d (list "e" "d" "c"))
Which list has at least 1 identical section in "main"?
Code: [Select]
b: no; three values are identical, but no pairs
c: yes (a - b)
d: no (because e - d - c are in wrong order)

Seem to be a kind of "search for identical pairs of values" ...

Any ideas?

Thanks
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Find identical list sections - compare lists?
« Reply #1 on: April 09, 2014, 02:14:43 PM »
Maybe something like so:


Code: [Select]
(setq main (list "a" "b" "c" "d" "e"))
(setq b (list "a" "c" "e"))
(setq c (list "a" "b" "e"))
(setq d (list "e" "d" "c"))


(defun _foo (mainlist otherlist / _topairs)
  (defun _topairs (l) (mapcar (function (lambda (a b) (cons a b))) l (cdr l)))
  (vl-remove-if-not
    (function (lambda (x) (vl-position x (_topairs otherlist))))
    (_topairs mainlist)
  )
)
(_foo main b)
(_foo main c)
(_foo main d)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Find identical list sections - compare lists?
« Reply #2 on: April 09, 2014, 07:12:44 PM »
A minor change to ron's:

Code: [Select]
(defun _foo ( x l )
    (setq l (mapcar 'list l (cdr l))
          x (mapcar 'list x (cdr x))
    )
    (vl-remove-if-not '(lambda ( x ) (member x l)) x)
)

Or, for a boolean return:

Code: [Select]
(defun bar ( x l / a b )
    (and (setq b (cadr x))
         (or (and (setq a (member (car x) l)) (equal b (cadr a)))
             (bar (cdr x) l)
         )
    )
)
Code: [Select]
(setq l '("a" "b" "c" "d" "e")
      b '("a" "c" "e")
      c '("a" "b" "e")
      d '("e" "d" "c")
)

_$ (bar b l)
nil
_$ (bar c l)
T
_$ (bar d l)
nil

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Find identical list sections - compare lists?
« Reply #3 on: April 10, 2014, 03:32:54 AM »
Untested, but isn't vl-position more efficient than member?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Find identical list sections - compare lists?
« Reply #4 on: April 10, 2014, 03:39:56 AM »
Untested, but isn't vl-position more efficient than member?
vl-position returns the index only, which doesn't suit the algorithm.
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.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Find identical list sections - compare lists?
« Reply #5 on: April 10, 2014, 04:01:21 AM »
Untested, but isn't vl-position more efficient than member?
vl-position returns the index only, which doesn't suit the algorithm.

This is correct for Lee's second example. The _foo function would also work with vl-position.

Peter2

  • Swamp Rat
  • Posts: 650
Re: Find identical list sections - compare lists?
« Reply #6 on: April 10, 2014, 11:29:10 AM »
Hi

many thank to all posters.

Every day I'm more impressed by the speed and the quality of the answers - and by the brevity of the code  :-)
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Find identical list sections - compare lists?
« Reply #7 on: April 12, 2014, 01:30:54 PM »
Untested, but isn't vl-position more efficient than member?

If I remember correctly, there is a very marginal improvement in performance for using vl-position over member, however, when testing whether an item is present in a given list, I personally find member more readable and more indicative of the intent of the code than vl-position.

@Peter, thank you for your kind words  :-)