how to test whether one list is a member of another

后端 未结 2 624
甜味超标
甜味超标 2021-01-18 14:45

Lets say I have two lists, ((1 2 3)) and (((1 2 3)) ((4 5))). I want to be able to tell if the first list is a member of the second list. I have t

2条回答
  •  不知归路
    2021-01-18 15:32

    If you want to have lists as elements of your sets for subsetp, you have to change the value of the :test keyword.

    CL-USER 1 > (subsetp '(1 2 3) '(1 2 3 4 5))
    T
    CL-USER 2 > (subsetp '((1) (2) (3)) '((1) (2) (3) (4) (5)))
    NIL
    

    The first one gives T, the second one gives NIL. Why? Because equality is checked with #'eql which works for identical objects or numbers of the same value and of same type. Since two lists must not be identical objects, (eql '(1) '(1)) gives NIL. (That may depend on your CL implementation.) If you want to compare a tree of conses, tree-equal can help you.

    CL-USER 3 > (subsetp '((1) (2) (3)) '((1) (2) (3) (4) (5)) :test #'tree-equal)
    T
    

    I don't understand the structure of the sets you gave as example completely, but I hope this helps.

提交回复
热议问题