Author Topic: Strange behavior function GC:INTERSECT  (Read 2303 times)

0 Members and 1 Guest are viewing this topic.

Giuseppe Beatrice

  • Newt
  • Posts: 43
Strange behavior function GC:INTERSECT
« on: January 18, 2019, 04:20:44 AM »
I greatly appreciated and used the Gile function GC:INTERSECT, but I noticed its anomalous behavior in one case, which I can not explain in any way:
(GC:INTERSECT '(3 3 2) '(7 3 2))
give the result (3 3 2) and not (3 2) as aspected.
I am confused,  I have the same result with function LM:LISTINTERSECTION of Lee-Mac  :oops:
Code: [Select]
;; gc:intersect
;; Retourne la liste des éléments communs à l1 et l2
;;
;; Arguments
;; l1 : une liste
;; l2 : une liste
(defun GC:INTERSECT  (l1 l2)
  (if l1
    (if   (member (car l1) l2)
      (cons (car l1) (gc:intersect (cdr l1) l2))
      (gc:intersect (cdr l1) l2))))

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: Strange behavior function GC:INTERSECT
« Reply #1 on: January 18, 2019, 04:51:06 AM »
Just apply (unique) sub on result you get :

Code: [Select]
(defun unique ( l )
  (if l
    (cons (car l)
      (unique (vl-remove (car l) l))
    )
  )
)

;; gc:intersect
;; Retourne la liste des éléments communs à l1 et l2
;;
;; Arguments
;; l1 : une liste
;; l2 : une liste
(defun GC:INTERSECT  (l1 l2)
  (if l1
    (if   (member (car l1) l2)
      (cons (car l1) (gc:intersect (cdr l1) l2))
      (gc:intersect (cdr l1) l2))))

Code: [Select]
(unique (GC:INTERSECT '(3 3 2) '(7 3 2)))

HTH., M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Strange behavior function GC:INTERSECT
« Reply #2 on: January 18, 2019, 05:33:19 AM »
Here's an alternative:
Code - Auto/Visual Lisp: [Select]
  1. (defun listinters ( a b / r )
  2.     (foreach x a
  3.         (and (member x b) (or (member x r) (setq r (cons x r))))
  4.     )
  5.     (reverse r)
  6. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (listinters '(3 3 2) '(7 3 2))
  2. (3 2)

Giuseppe Beatrice

  • Newt
  • Posts: 43
Re: Strange behavior function GC:INTERSECT
« Reply #3 on: January 18, 2019, 09:42:24 AM »
Thank you very much, Ribarm and Lee-Mac, but I do not think the proposed solutions can be accepted.
In fact, they fail if the lists to be compared contain both multiple terms, e.g. '(3 3 3 3 2) and ' (3 3 7 2) for which the correct result is (3 3 2).
I think the solution could be the following correction of the function GC:INTERSECT, which I built with the function GC:REMOVEFIRST

Code: [Select]
(defun GC: INTERSECT (l1 l2 / ec)
   (if l1
     (if (member (setq and c (car l1)) l2)
       (progn (setq l2 (GC: REMOVEFIRST and c l2))
(cons ec (GC: INTERSECT (cdr l1) l2)))
       (GC: INTERSECT (cdr l1) l2))))

(defun GC:REMOVEFIRST  (ele lst)
  (if (equal ele (car lst))
    (cdr lst)
    (cons (car lst) (GC:REMOVEFIRST ele (cdr lst)))))

(GC: INTERSECT '(3 3 3 3 2)' (3 3 7 2)) -> (3 3 2)
« Last Edit: January 18, 2019, 10:03:01 AM by Giuseppe Beatrice »

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Strange behavior function GC:INTERSECT
« Reply #4 on: January 18, 2019, 10:07:03 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun listinters ( a b / f r )
  2.     (foreach x a
  3.         (setq b (vl-remove-if '(lambda ( y ) (cond (f nil) ((setq f (= x y)) (setq r (cons x r))))) b)
  4.               f nil
  5.         )
  6.     )
  7.     (reverse r)
  8. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (listinters '(3 3 3 3 2)' (3 3 7 2))
  2. (3 3 2)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Strange behavior function GC:INTERSECT
« Reply #5 on: January 18, 2019, 10:28:12 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun listinters ( a b )
  2.     (vl-remove-if-not '(lambda ( x / f ) (setq b (vl-remove-if '(lambda ( y ) (cond (f nil) ((setq f (= x y))))) b)) f) a)
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (listinters '(3 3 3 3 2)' (3 3 7 2))
  2. (3 3 2)

Giuseppe Beatrice

  • Newt
  • Posts: 43
Re: Strange behavior function GC:INTERSECT
« Reply #6 on: January 19, 2019, 06:23:06 AM »
Thank you Lee, you are my best master.