TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Peter2 on April 09, 2014, 12:42:03 PM

Title: Find identical list sections - compare lists?
Post by: Peter2 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
Title: Re: Find identical list sections - compare lists?
Post by: ronjonp 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)
Title: Re: Find identical list sections - compare lists?
Post by: Lee Mac 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
Title: Re: Find identical list sections - compare lists?
Post by: roy_043 on April 10, 2014, 03:32:54 AM
Untested, but isn't vl-position more efficient than member?
Title: Re: Find identical list sections - compare lists?
Post by: Kerry 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.
Title: Re: Find identical list sections - compare lists?
Post by: roy_043 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.
Title: Re: Find identical list sections - compare lists?
Post by: Peter2 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  :-)
Title: Re: Find identical list sections - compare lists?
Post by: Lee Mac 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  :-)