我是prolog的新手并试图弄清楚如何解决这个问题 编号1,编号2和编号3是蛋糕名称 巧克力,芒果,柠檬...之后的成分和数字是我不需要用于此查询的价格,但它们必须在那里。
icecream(number1, [chocolate,mango,lemon], 1.99).
icecream(number2, [vanille,lemon ], 1.79).
icecream(number3, [almonds, kiwi, walnut], 2.50).
查询:
which_icecream( [vanille,lemon,chocolate,mango], S).
S = number1;
S = number2.
谢谢!
答案 0 :(得分:0)
对非确定性交叉使用member/2
两次:
which_icecream(DesiredIngredients, S) :-
icecream(S, Ingredients, _),
member(X, DesiredIngredients),
member(X, Ingredients).
请注意,由于许多成分会多次匹配,因此会多次返回相同的结果:
?- which_icecream([vanille,lemon,chocolate,mango], S).
S = number1 ;
S = number1 ;
S = number1 ;
S = number2 ;
S = number2 ;
false.
使用setof/3
?- setof(S, which_icecream([vanille,lemon,chocolate,mango], S), IceCreams).
IceCreams = [number1, number2].
另一种解决方案,使用intersection/3
内置:
which_icecream(DesiredIngredients, S) :-
icecream(S, Ingredients, _),
intersection(Ingredients, DesiredIngredients, Intersection),
Intersection \= [].