开发者

List order sensitivity

开发者 https://www.devze.com 2023-02-02 20:01 出处:网络
Given fact likes([apples, oranges], john). how could I query likes([apples, oranges], Who). and likes([oranges, apples], Who).

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).
0

精彩评论

暂无评论...
验证码 换一张
取 消