Вопрос

I'm experiencing a very curious problem. I have a list named "theorems" that has exactly one item. Here's proof:

[]> theorems
(((ROSES ARE RED) ^ (~ (ROSES ARE RED))))
[]> (car theorems)
((ROSES ARE RED) ^ (~ (ROSES ARE RED)))

Clearly ((ROSES ARE RED) ^ (~ (ROSES ARE RED))) is a member of the list "theorems." But when I test it for membership, it fails:

[]> (member '((ROSES ARE RED) ^ (~ (ROSES ARE RED))) theorems)
NIL

But if I call it explicitly, it works:

[]> (member (car theorems) theorems)
(((ROSES ARE RED) ^ (~ (ROSES ARE RED))))

Why is this happening, and how can I fix it?

Это было полезно?

Решение

Common Lisp uses EQL as the default test function. EQL checks whether the items are the same identical items. You want to test whether the items have the same structure. So you need to use EQUAL or EQUALP.

CL-USER 11 > (setf theorems '(((ROSES ARE RED) ^ (~ (ROSES ARE RED)))))
(((ROSES ARE RED) ^ (~ (ROSES ARE RED))))

CL-USER 12 > (member '((ROSES ARE RED) ^ (~ (ROSES ARE RED))) theorems)
NIL

Tell MEMBER to use EQUAL:

CL-USER 13 > (member '((ROSES ARE RED) ^ (~ (ROSES ARE RED)))
                     theorems
                     :test 'equal)
(((ROSES ARE RED) ^ (~ (ROSES ARE RED))))
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top