List Difference Functions
See also List Union, List Intersection & List Symmetric Difference
Difference of Two Lists
Function Syntax | (LM:ListDifference <l1> <l2>) |
Current Version | 1.0 |
Arguments | ||
---|---|---|
Symbol | Type | Description |
l1,l2 | List | Lists for which to return the Difference |
Returns | ||
Type | Description | |
List | List of items appearing exclusively in list l1 |
Program Description
This subfunction will return a list expressing the difference or relative complement of two lists, that is, a list of items appearing exclusively in one list but not another.

Recursive Version
Select all
;;-------------------=={ List Difference }==------------------;; ;; ;; ;; Returns items appearing exclusively in one list but not ;; ;; another, i.e. the relative complement: l1 \ l2 ;; ;;------------------------------------------------------------;; ;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;; ;;------------------------------------------------------------;; ;; Arguments: ;; ;; l1,l2 - lists for which to return the difference ;; ;;------------------------------------------------------------;; ;; Returns: List of items appearing exclusively in list l1 ;; ;;------------------------------------------------------------;; (defun LM:ListDifference ( l1 l2 ) (if l1 (if (member (car l1) l2) (LM:ListDifference (cdr l1) l2) (cons (car l1) (LM:ListDifference (cdr l1) l2)) ) ) )
Iterative Version
Select all
;;-------------------=={ List Difference }==------------------;; ;; ;; ;; Returns items appearing exclusively in one list but not ;; ;; another, i.e. the relative complement: l1 \ l2 ;; ;;------------------------------------------------------------;; ;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;; ;;------------------------------------------------------------;; ;; Arguments: ;; ;; l1,l2 - lists for which to return the difference ;; ;;------------------------------------------------------------;; ;; Returns: List of items appearing exclusively in list l1 ;; ;;------------------------------------------------------------;; (defun LM:ListDifference ( l1 l2 ) (vl-remove-if '(lambda ( x ) (member x l2)) l1) )
Example Function Call
_$ (LM:ListDifference '(1 2 3 4 5) '(2 4 6)) (1 3 5)
Difference of a Set of Lists
Function Syntax | (LM:ListsDifference <l1> <ls>) |
Current Version | 1.0 |
Arguments | ||
---|---|---|
Symbol | Type | Description |
l1 | List | List from which items are to be subtracted |
ls | List | List of lists whose items may be subtracted from l1 |
Returns | ||
Type | Description | |
List | List of items appearing exclusively in list l1 |
Program Description
This subfunction will return a list expressing the difference or relative complement of a list with a set of lists, that is, a list of items appearing exclusively in one list but not any other.

Recursive Version
Select all
;;-------------------=={ Lists Difference }==-----------------;; ;; ;; ;; Returns items appearing exclusively in one list but not ;; ;; others, i.e. the relative complement: l1\l2\l3\...\lN ;; ;;------------------------------------------------------------;; ;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;; ;;------------------------------------------------------------;; ;; Arguments: ;; ;; l1 - list from which items are to be subtracted ;; ;; ls - list of lists whose items may be subtracted from l1 ;; ;;------------------------------------------------------------;; ;; Returns: List of items appearing exclusively in list l1 ;; ;;------------------------------------------------------------;; (defun LM:ListsDifference ( l1 ls ) (if l1 (if (vl-some '(lambda ( l ) (member (car l1) l)) ls) (LM:ListsDifference (cdr l1) ls) (cons (car l1) (LM:ListsDifference (cdr l1) ls)) ) ) )
Iterative Version
Select all
;;-------------------=={ Lists Difference }==-----------------;; ;; ;; ;; Returns items appearing exclusively in one list but not ;; ;; others, i.e. the relative complement: l1\l2\l3\...\lN ;; ;;------------------------------------------------------------;; ;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;; ;;------------------------------------------------------------;; ;; Arguments: ;; ;; l1 - list from which items are to be subtracted ;; ;; ls - list of lists whose items may be subtracted from l1 ;; ;;------------------------------------------------------------;; ;; Returns: List of items appearing exclusively in list l1 ;; ;;------------------------------------------------------------;; (defun LM:ListsDifference ( l1 ls ) (vl-remove-if '(lambda ( x ) (vl-some '(lambda ( l ) (member x l)) ls)) l1) )
Version Derived from Set Operations
Requires Lists Union & List Difference subfunctions
Select all
(defun LM:ListsDifference ( l1 ls ) (LM:ListDifference l1 (LM:ListsUnion ls)) )
Example Function Call
_$ (LM:ListsDifference '(1 2 3 4 5 6 7 8 9) '((2 4 6) (1 2 3 5 8))) (7 9)
See also List Union, List Intersection & List Symmetric Difference