试图解决一个简单的prolog任务

时间:2013-04-03 10:57:07

标签: prolog

这是我的作业线索:

四名车手:诺玛,安迪,爱德华和奥利维亚被指控违反了四项违规行为:停车标志,超速驾驶,非法左转,以及发出红灯。利用线索找出每个嫌疑人违反的法律。

线索

  1. 诺玛没有跑红灯或停车标志。
  2. Andy从来没有因超速而停下来。
  3. 爱德华非法左转。
  4. Olivia总是小心翼翼地停在停车标志处。
  5. 我需要使用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的新手!

1 个答案:

答案 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):-