List Intersection Functions

See also List Union, List Difference & List Symmetric Difference

Intersection of Two Lists

Function Syntax (LM:ListIntersection <l1> <l2>)
Current Version 1.0
Arguments
Symbol Type Description
l1,l2 List Lists for which to return the Intersection
Returns
Type Description
List A list of all items appearing in both lists

Program Description

This subfunction will return a list expressing the intersection of two lists, that is, a list of items common to both of the supplied lists.

Intersection.png

Recursive Version

Select all
;;-----------------=={ List Intersection }==------------------;;
;;                                                            ;;
;;  Returns a list expressing the intersection of two lists   ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright  2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  l1,l2 - Lists for which to return the intersection        ;;
;;------------------------------------------------------------;;
;;  Returns:  A list of all items common to both lists        ;;
;;------------------------------------------------------------;;

(defun LM:ListIntersection ( l1 l2 )
  (if l1
    (if (member (car l1) l2)
      (cons (car l1) (LM:ListIntersection (cdr l1) l2))
      (LM:ListIntersection (cdr l1) l2)
    )
  )
)

Iterative Version

Select all
;;-----------------=={ List Intersection }==------------------;;
;;                                                            ;;
;;  Returns a list expressing the intersection of two lists   ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright  2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  l1,l2 - Lists for which to return the intersection        ;;
;;------------------------------------------------------------;;
;;  Returns:  A list of all items common to both lists        ;;
;;------------------------------------------------------------;;

(defun LM:ListIntersection ( l1 l2 )
  (vl-remove-if-not '(lambda ( x ) (member x l2)) l1)
)

Example Function Call

_$ (LM:ListIntersection '(1 2 3 4 5) '(2 4 6 8))
(2 4)

Intersection of a Set of Lists

Function Syntax (LM:ListsIntersection <l>)
Current Version 1.0
Arguments
Symbol Type Description
l List A list of lists for which to return the intersection
Returns
Type Description
List A list of items appearing in every list

Program Description

This subfunction will return a list expressing the intersection of a set of lists, that is, a list of all items appearing in every sublist in a supplied list, hence items common to all lists.

IntersectionM.png

Recursive Version

Select all
;;-----------------=={ Lists Intersection }==-----------------;;
;;                                                            ;;
;;  Returns a list of the intersection of a set of lists      ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright  2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  l - a list of lists for which to return the intersection  ;;
;;------------------------------------------------------------;;
;;  Returns:  A list of items common to every list            ;;
;;------------------------------------------------------------;;

(defun LM:ListsIntersection ( l / sub )
  (defun sub ( a b )
    (if a
      (if (vl-every '(lambda ( x ) (member (car a) x)) b)
        (cons (car a) (sub (cdr a) b))
        (sub (cdr a) b)
      )
    )
  )
  (sub (car l) (cdr l))
)

Iterative Version

Select all
;;-----------------=={ Lists Intersection }==-----------------;;
;;                                                            ;;
;;  Returns a list of the intersection of a set of lists      ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright  2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  l - a list of lists for which to return the intersection  ;;
;;------------------------------------------------------------;;
;;  Returns:  A list of items common to every list            ;;
;;------------------------------------------------------------;;

(defun LM:ListsIntersection ( l )
  (vl-remove-if-not '(lambda ( a ) (vl-every '(lambda ( b ) (member a b)) (cdr l))) (car l))
)

Example Function Call

_$ (LM:ListsIntersection '((1 2 3 4 5) (1 3 5 7 9) (1 2 3 5 8)))
(1 3 5)

See also List Union, List Difference & List Symmetric Difference

textsize

increase · reset · decrease

Designed & Created by Lee Mac © 2010