这是我的作业线索:
四名车手:诺玛,安迪,爱德华和奥利维亚被指控违反了四项违规行为:停车标志,超速驾驶,非法左转,以及发出红灯。利用线索找出每个嫌疑人违反的法律。
线索
我需要使用prolog来解决它。
到目前为止,我已尝试过以下代码:
violatated(Violation):-
member(Violation,[speeding,leftturn,stop,red]).
solve(S):-
S=[[Name1,Violation1],
[Name2,Violation2],
[Name3,Violation3],
[Name4,Violation4]],
%five drivers: Norma, Andy, Edward, Olivia
Name1 = norma,
Name2 = andy,
Name3 = edward,
Name4 = olivia,
violated(Violation1), violated(Violation2), violated(Violation3), violated(Violation4),
Violation1 \== Violation2, Violation1 \== Violation3, Violation1 \== Violation4,
Violation2 \== Violation1, Violation2 \== Violation3, Violation2 \== Violation4,
Violation3 \== Violation1, Violation3 \== Violation2, Violation3 \== Violation4,
Violation4 \== Violation1, Violation4 \== Violation2, Violation4 \== Violation3,
%Edward made an illegal left turn
Violation3 == leftturn,
%Norma did not run either a red light or a stop sign
Violation1 \== red,
Violation1 \== stop,
%Andy was never stopped for speeding.
Violation2 \== speeding,
%Olivia was always careful to stop at a stop sign
Violation4 \== stop.
请原谅我是prolog的新手!
答案 0 :(得分:0)
我简化了你的代码:
solve(S):-
S=[[norma,Violation1],
[andy,Violation2],
[edward,Violation3],
[olivia,Violation4]],
permutation([speeding,leftturn,stop,red], [Violation1,Violation2,Violation3,Violation4]),
%Edward made an illegal left turn
Violation3 = leftturn,
%Norma did not run either a red light or a stop sign
Violation1 \= red,
Violation1 \= stop,
%Andy was never stopped for speeding.
Violation2 \= speeding,
%Olivia was always careful to stop at a stop sign
Violation4 \= stop.
与此:
?- solve(S).
S = [[norma, speeding], [andy, stop], [edward, leftturn], [olivia, red]] ;
false.
但你的代码似乎没问题,只是一个错误的纠正:
violated(Violation):-
member(Violation,[speeding,leftturn,stop,red]).
而不是violatated(Violation):-