Prolog如何以这种方式使用if / else?

时间:2015-07-11 13:36:35

标签: if-statement prolog

在Prolog中,我想以下列方式使用if-else语句:

lecture(spanish,monday,at8).
lecture(french,monday,at8).
lecture(english,monday,at10).
lecture(german,tuesday,at8).

generatePossibleSchedulesWithTwoLectures(LectureA,LectureB):-
     lecture(LectureA,A,X),
     lecture(LectureB,B,Y),
     %If A of LectureA is equal to B of lectureB -> times must be un-equal
     % How to do implement the requirement of the line above here?

因此,该函数应生成两个讲座的时间表,这两个讲座不在同一时段。

1 个答案:

答案 0 :(得分:2)

您首先假设,我想以下列方式使用if-else语句,但您的问题逻辑并未描述if-then-else条件。 if-then-else不是您希望实现目标的构造:

  

使用两个不在同一时段的讲座生成时间表

此外,Prolog if-then-else构造A -> B ; C删除了您所选择的选择点。有关详细信息,请参阅What's the meaning of Prolog operator '->'

的答案

你真正想要的是:

安排两个讲座A& B有效如果 Day1Time1A Day2的时段Time2B时段Day1-Time1Day2-Time2不同的时段。

如果事实是,为避免重复并按时间顺序保持计划,我们可以强制执行“第一”时间段早于第二时间段。这转化为:

generate_possible_schedules_with_two_lectures(LectureA, LectureB):-
    lecture(LectureA, Day1, Time1),
    lecture(LectureB, Day2, Time2),
    dif(LectureA, LectureB),   % don't schedule the same class
    Day1-Time1 @< Day2-Time2   % timeslots in order, and different

我们通过与日期和时间形成一个术语A-B来简化时段比较。您可以进行较长时间的比较,单独比较日期和领带,将Day1-Time1 @< Day2-Time2替换为(Day1 @< Day2 ; Day1 == Day2, Time1 @< Time2)。在任何一种情况下,您都会得到以下数据结果:

?- generate_possible_schedules_with_two_lectures(A, B).
A = spanish,
B = german ;
A = french,
B = german ;
A = english,
B = spanish ;
A = english,
B = french ;
A = english,
B = german ;
false.
相关问题