neigh(Left, Right, List) :-
List = [Left, Right ,_];
List = [_, Left, Right]].
但只适用于三栋房子。更通用的实现是:
neigh(Left, Right, List) :-
List = [Left , Right | _].
neigh(Left, Right, [_|Tail]) :-
neigh(Left, Right, Tail).
I'm new to Prolog, but I think the definition of neigh is not quite right.Try:
neigh(2,3,[1,2,3]).
You get away with this not quite working because there are two solutions, one with the Japanese owned zebra in the second house, and one with the zebra in the third house and your code only finds one (which is enough to answer the question :-). This code give the right answers for neigh and hence both answers to the problem:
neigh(Left, Right, List) :-
List = [Left, Right ,_];
List = [_, Left, Right]].
but then only works for three houses. A more general implementation is:
neigh(Left, Right, List) :-
List = [Left , Right | _].
neigh(Left, Right, [_|Tail]) :-
neigh(Left, Right, Tail).
发布评论
评论(2)
我是 Prolog 的新手,但我认为 neigh 的定义不太正确。尝试:
你可以摆脱这个不太有效的问题,因为有两种解决方案,一种是日本拥有的斑马在第二个房子,另一种是斑马在第三宫,而你的代码只找到一个(这足以回答问题:-)。
这段代码给出了 neigh 的正确答案,因此给出了问题的两个答案:
但只适用于三栋房子。更通用的实现是:
I'm new to Prolog, but I think the definition of neigh is not quite right.Try:
You get away with this not quite working because there are two solutions, one with the Japanese owned zebra in the second house, and one with the zebra in the third house and your code only finds one (which is enough to answer the question :-).
This code give the right answers for neigh and hence both answers to the problem:
but then only works for three houses. A more general implementation is: