Search code examples
listprolog

How can I get a list of facts with an id matching each fact list item with same id using Prolog?


student(2000100001,1,[math101, phys201, ec201]).
student(2000100002,1,[math101, phys201, hist301]).
student(2000100003,1,[physics201, ec201, hist301]) 

I need the predicate list_student(course_id,list_e). gets course_id and add to list_e all students that have that course. Result Should be:

list_students(math101, L).
L = [2000100001, 2000100002]

Solution

  • I propose some hints to get you started.

    1. You have to iterate over each student clause to get each course he is doing and store the data in the reverse fashion: course --> list of students. Simplest ways of doing 1. you can store the data in the internal database, i.e. for each student assert the fact course(courseId, studentId). There are other ways.

    2. Recuperate the data course --> list of students to a list. For each course, find the list of students using a findall. The result is asserted to the database list_(courseId, listOfStudents).

    Try this and report back here!