Note that there are some explanatory texts on larger screens.

plurals
  1. POIt is possible to manipulate a list generated with findall?
    primarykey
    data
    text
    <p>The context is the following in my program: I have some <code>students</code>, of a certain <code>country</code> and with certain <code>years</code> of study, as follows in this prolog code:</p> <pre><code>student('Steve Morris'). student('Joe Jez'). student('Carlos Sethi'). student('Natasha Carter'). country('Steve Morris', usa). country('Joe Jez', usa). country('Carlos Sethi', usa). country('Natasha Carter', france). years('Steve Morris', 3). years('Joe Jez', 1). years('Carlos Sethi', 4). years('Natasha Carter', 4). scholarship(A) :- country(A,B), B = france. scholarship(A) :- years(A,C), C &gt; 2. </code></pre> <p>I want to give one and just one scholarship to one of my students. To do this, I will use <strong>some rules that will raise the "scholarship factor"</strong>, and <strong>the student who gets the greater scholarship factor will get the scholarship</strong>. </p> <p>The first rule states that the student must come from France, and the second rule states that the student must have more than two years of study. </p> <p>So, when I execute <code>scholarship(X)</code>, this is what I get:</p> <pre><code>?- scholarship(X). X = 'Natasha Carter' ; % Only student who matches the first rule X = 'Steve Morris' ; % All students from now on, match the second rule X = 'Carlos Sethi' ; X = 'Natasha Carter'. </code></pre> <p>With that said, <strong>I'm trying to do a program that tries to get the name of the student who finally gets the scholarship</strong>. To start, I've tried first to execute a predicate <code>findall</code> to filter all the student that match these rules one by one, and put it on a list:</p> <pre><code>?- findall(X, scholarship(X), L). L = ['Natasha Carter', 'Steve Morris', 'Carlos Sethi', 'Natasha Carter']. </code></pre> <p>Which is an expected result, because of the use of <code>scholarship(X)</code>. </p> <p>Now, looks like I will need the generated list to filter the results and satisfy what I'm looking for. Take in mind that in the example above, the result that I'm expecting to reach, at least, is a list which indicates the student and his scholarship factor, something like this (not necessarily exact):</p> <pre><code>[['Natasha Carter', 2], ['Steve Morris', 1], ['Carlos Sethi', 1]]. </code></pre> <p>It is a way to manipulate the list generated with findall? Or I certainly need another way to solve this?</p> <p><strong>Edit:</strong> There's something important about the problem modeling: <strong>All rules have the same scholarship factor value</strong>, so when a student satisfy a rule, no matter which one, the scholarship factor should rise up to 1.</p> <p><strong>Updates to the problem</strong>: Thanks to Mog I have an approach of the problem, applying <code>msort/2</code> using a secondary list, this is what I've got:</p> <pre><code>?- findall(X, scholarship(X), L), msort(L, L1). L = ['Natasha Carter', 'Steve Morris', 'Carlos Sethi', 'Natasha Carter'], L1 = ['Carlos Sethi', 'Natasha Carter', 'Natasha Carter', 'Steve Morris']. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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