Note that there are some explanatory texts on larger screens.

plurals
  1. POOutput only the sad students from a list in Prolog
    text
    copied!<p>I have the following declaration:</p> <pre><code>calculate_sad_moods([]):-!. calculate_sad_moods([X2|Rest2]) :- (sad(X2),write(`One of our sad students is: `),write(X2),nl); calculate_sad_moods(Rest2). </code></pre> <p>When I call it like this:</p> <pre><code>?- calculate_sad_moods([ ivan, dragan, petkan, borislav, damyan, ani, petyr, ivo ]). </code></pre> <p>I end up with a very long list like this (It's ok, but it has dublicate records):</p> <pre><code>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 </code></pre> <p>When I change the declaration to:</p> <pre><code>calculate_sad_moods([]):-!. calculate_sad_moods([X2|Rest2]) :- write(`One of our sad students is: `),write(X2),nl, calculate_sad_moods(Rest2). </code></pre> <p>I end up with this (It's not OK, because not everybody is sad, but it has no dublicate records):</p> <pre><code>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 </code></pre> <p>How can I output only the sad students and only once? I've tried adding brackets and several other tricks and still - Nothing. I have no other ideas left where to look.</p> <p><strong>P.S: Here's how the sad declaration looks like</strong></p> <pre><code>[...] 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))). [...] </code></pre> <p>It'is still very obscure, but I'm still learning the basics...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload