Counting Prolog answers for a recursive rule

A Prolog program records which course is a direct prerequisite of another and defines a recursive rule for indirect prerequisites.


prolog
prereq(cs101, cs201).
prereq(cs101, cs202).
prereq(cs201, cs301).
prereq(cs202, cs301).
prereq(cs202, cs302).

leads_to(A, C) :- prereq(A, C).
leads_to(A, C) :- prereq(A, B), leads_to(B, C).


The query ?- leads_to(cs101, C). is entered, and the user keeps asking for more answers until Prolog reports there are no more. Prolog uses its standard search: clauses top to bottom, goals left to right, with backtracking. How many answers for C does Prolog report in total, counting any repeated value each time it appears? Give your answer as a whole number.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Procedural, Functional, Logical and Object-Oriented Paradigms Questions: