harrison-matt Posted June 4, 2011 Posted June 4, 2011 All, How should I go about verifying that an item in one list does not appear in another list? Kind Regards, Matt Quote
Tharwat Posted June 4, 2011 Posted June 4, 2011 by function *member* you can check that out . (setq lst1 '(1 2 3 4 5 )) (setq lst2 '(6 7 8 9 10)) (setq a (not (member 8 lst1 ))) Quote
Lee Mac Posted June 4, 2011 Posted June 4, 2011 An example of a function to return the 'intersection' of two 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) ) ) ) And its complement, the 'union' of two lists: (defun LM:ListUnion ( l1 l2 ) ( (lambda ( f ) (f (append l1 l2))) (lambda ( l ) (if l (cons (car l) (f (vl-remove (car l) (cdr l)))))) ) ) Quote
irneb Posted June 6, 2011 Posted June 6, 2011 My versions: ;|--------------------------------------------------------------------------------------- = List:UniqueP = Test if a list contains only unique elements === Arguments === L: The source list === Result === T if unique list, nil otherwise ---------------------------------------------------------------------------------------|; (defun List:UniqueP (L /) (while (and L (not (vl-position (car L) (cdr L)))) (setq L (cdr L))) (not L) ) ;|--------------------------------------------------------------------------------------- = List:Unique = Ensure only unique values in list. === Arguments === L: The source list === Result === A new list containing only unique elements from source ---------------------------------------------------------------------------------------|; (defun List:Unique (L /) (if (cdr L) (if (vl-position (car L) (cdr L)) (List:Unique (cdr L)) (cons (car L) (List:Unique (cdr L))) ) L ) ) ;|--------------------------------------------------------------------------------------- = List:Subtract = Removes elements from one list if they are in another. === Arguments === L1: The source list L2: The list to subtract from L1 === Result === A new list containing all elements of L1 which were not in L2 ---------------------------------------------------------------------------------------|; (defun List:Subtract (L1 L2 /) (vl-remove-if '(lambda (item) (vl-position item L2)) (List:Unique L1)) ) ;|--------------------------------------------------------------------------------------- = List:Union = Combines 2 lists together === Arguments === L1, L2: The 2 source lists === Result === A new list containing all elements of L1 and L2 - no duplicates ---------------------------------------------------------------------------------------|; (defun List:Union (L1 L2 /) (append (List:Subtract (List:Unique L1) (setq L2 (List:Unique L2))) L2) ) ;|--------------------------------------------------------------------------------------- = List:Intersect = Intersects 2 lists together === Arguments === L1, L2: The 2 source lists === Result === A new list containing all elements found in both L1 and L2 - no duplicates ---------------------------------------------------------------------------------------|; (defun List:Intersect (L1 L2 /) (vl-remove-if-not '(lambda (item) (vl-position item L2)) (List:Unique L1)) ) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.