Why does prolog answer false to: member([5], [2, 5]).
?
Is there a way to work around it?
Any help will be greatly appreciated.
Why does Prolog answer false to:
member([5], [2, 5]).
?
Let's ask Prolog why! Download library(diadem)
into your working directory and:
?- use_module(diadem).
true.
?- member([5], [2,5]).? Expl.
Expl = member([_|_], [2, 5])
; ...
Not only does it fail but also a generalization fails:instead of [5]
that is a list with a single element 5, we have now simply at least one element - no matter which. So we can take the value for Expl
as a query which still fails. Something in the remaining goal must thus be the culprit.
?- member([_|_], [2, 5]).
false.
Note also what was not generalized away: The two elements are still here! If they would be variables, the query would succeed! Generalize the query a bit:
?- member([5], [2, Any]).
Any = [5].
Now it succeeds!
As described in the SWI Prolog documentation for member/2 :
member( ?Elem , ?List )
True if
Elem
is a member ofList
.
The predicate is member . It is not subset or sublist or subsequence . It succeeds if the first argument is a member (that is, an element ) of the list given in the second argument. The element [5]
is not a member of the list [2, 5]
since the element [5]
isn't 2 and it isn't 5. However, [5]
would be a member of the list [2, [5]]
, so member([5], [2, [5]])
would succeed.
It could be that you are looking for another predicate?
From the SWI Prolog documentation for subset/2 :
subset( +SubSet , +Set )
True if all elements of SubSet belong to Set as well.
It works as expected:
Welcome to SWI-Prolog (threaded, 64 bits, version 7.5.5)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
?- subset([5], [2, 5]).
true.
?- subset([5, 3], [2, 3, 5, 7]).
true.
?- subset([5, 3], [2, 5]).
false.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.