Given fact
likes([apples, oranges], john).
how could I query
likes([apples, oranges], Who).
and
likes([oranges, apples], Who).
and g开发者_C百科et same result?
If you don't want to sort, I would do something like that:
File:
likes([apples, oranges], jo).
likes_find([],_).
likes_find([Head | Tail] , Who):-
likes(List1 , Who),
member(Head , List1),
likes_find(Tail ,Who).
Test:
?- likes_find([oranges,apples],X).
X = jo .
?- likes_find([oranges,apples,fail_here_plz],X).
false.
?- likes_find([oranges],X).
X = jo .
?- likes_find([oranges,apples],jo).
true .
?- likes_find([apples,oranges],jo).
true .
If, in likes/2
, the first (list) argument is always manually sorted and contains no repetitions, query using
likes_list(Stuff,Person) :- sort(Stuff,Sorted), likes(Sorted,Person).
精彩评论