我有以下声明:
calculate_sad_moods([]):-!.
calculate_sad_moods([X2|Rest2]) :-
(sad(X2),write(`One of our sad students is: `),write(X2),nl);
calculate_sad_moods(Rest2).
当我这样称呼它时:
?- calculate_sad_moods([
ivan, dragan, petkan, borislav, damyan, ani, petyr, ivo
]).
我最终得到了一个非常长的列表(没关系,但它有重复的记录):
One of our sad students is: ivan
Yes.
One of our sad students is: dragan
Yes.
One of our sad students is: dragan
Yes.
One of our sad students is: petkan
Yes.
One of our sad students is: petkan
Yes.
One of our sad students is: borislav
Yes.
One of our sad students is: borislav
Yes.
One of our sad students is: damyan
Yes.
One of our sad students is: damyan
Yes.
One of our sad students is: damyan
Yes.
One of our sad students is: ani
Yes.
One of our sad students is: ani
Yes.
One of our sad students is: ani
Yes.
One of our sad students is: petyr
Yes.
One of our sad students is: petyr
当我将声明更改为:
calculate_sad_moods([]):-!.
calculate_sad_moods([X2|Rest2]) :-
write(`One of our sad students is: `),write(X2),nl,
calculate_sad_moods(Rest2).
我最终得到了这个(这不好,因为不是每个人都很难过,但它没有重复记录):
One of our sad students is: ivan
One of our sad students is: dragan
One of our sad students is: petkan
One of our sad students is: borislav
One of our sad students is: damyan
One of our sad students is: ani
One of our sad students is: petyr
One of our sad students is: ivo
我怎样才能只输出悲伤的学生并且只输出一次?我试过添加括号和其他一些技巧,但仍然 - 没有。我没有其他想法可以去哪里看。
PS:这是悲伤声明的样子
[...]
sad(damyan) :-
(not(lovesExam(damyan,chemistry)),hasExam(damyan,chemistry));
(not(lovesExam(damyan,biology),hasExam(damyan,biology)));
(not(lovesExam(damyan,math),hasExam(damyan,math))).
sad(ani) :-
(not(lovesExam(ani,chemistry)),hasExam(ani,chemistry));
(not(lovesExam(ani,biology),hasExam(ani,biology)));
(not(lovesExam(ani,math),hasExam(ani,math))).
[...]
它仍然很晦涩,但我仍在学习基础知识...