- 下面给出的if else表示是否正确?
- 我希望if-else语句解释为:
如果成绩是NC且课程是CDC,则调用isNCinCDC或
如果成绩是NC并且课程是研讨会,那么请致电isNCinCDC或
如果成绩是NC而课程是论文,则调用isNCinCDC或
如果成绩是NC并且课程是选修课,那么请致电isNCinELEC或
如果成绩是NC,课程是练习学校,那么打电话给NinPS或
如果成绩是NC而课程是项目课程,那么请致电NinPC
isNCCalc([],[],[]) :-
display('There are no more courses to check for NC').
isNCCalc([G|T1],[S|T2],[C|T3]) :-
(((G == 'NC' , C == 'CDC') -> isNCinCDC(S)) ;
((G == 'NC',C == 'SEMINAR'-> isNCinCDC(S)) ;
((G == 'NC',C == 'THESIS' -> isNCinCDC(S)) ;
((G == 'NC',C == 'ELECTIVES' -> isNCinELEC(S)) ;
((G == 'NC',C == 'PRACTICE SCHOOL' -> isNCinPS(S)) ;
((G == 'NC',C == 'PROJECT COURSE' -> isNCinPC(S)))),
isNCCalc(T1,T2,T3).
答案 0 :(得分:1)
你可以更简单地写它,因为
3 ?- display((a,b->c)).
->(,(a,b),c)
true.
4 ?- display(((a,b)->c)).
->(,(a,b),c)
true.
即。连词(逗号)绑定比暗示(箭头)更严格。然后
isNCCalc([G|T1],[S|T2],[C|T3]) :-
( G == 'NC', C == 'CDC'
-> isNCinCDC(S)
; G == 'NC', C == 'SEMINAR'
-> isNCinCDC(S)
; G == 'NC', C == 'THESIS'
-> isNCinCDC(S)
; G == 'NC', C == 'ELECTIVES'
-> isNCinELEC(S)
; G == 'NC', C == 'PRACTICE SCHOOL'
-> isNCinPS(S)
; G == 'NC', C == 'PROJECT COURSE'
-> isNCinPC(S)
; true
),
isNCCalc(T1,T2,T3).
我添加了true作为最后一个选项,因为你可以在头部移动G =='NC',并简化更多,或者当G \ ='NC'出现时,循环将完全失败。不确定,在这种情况下你没有指定你想要的东西。