TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started 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
(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"?
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
-
Maybe something like so:
(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)
-
A minor change to ron's:
(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:
(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)
)
)
)
(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
-
Untested, but isn't vl-position more efficient than member?
-
Untested, but isn't vl-position more efficient than member?
vl-position returns the index only, which doesn't suit the algorithm.
-
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.
-
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 :-)
-
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 :-)