1
votes

Here I have a list of modules rule.

module(oop). % Object Oriented Programming
module(se1). % Software Engineering 1
module(se2). % Software Engineering 2
% etc.

Then I defined the prerequisites for each module.

require(oop, []). % oop is base module. It doesn't require any other modules.
require(se1, [oop]). % se1 requires oop
require(se2, [se1]). % se2 requires se1

As you can see, se2 requires se1 (directly) and oop (indirectly). I would like to define a function which goes through the list recursively and return all the required modules as a list.

Query:

?- dependent(se2, L).

Expected Result:

L = [se1, oop]

How can I write the dependent predicate in Prolog using recursion?

Thank you very much for your help.

1
How can write the dependent function: Prolog has no functions only predicates...coder

1 Answers

1
votes

An easy-obvious way using mutual recursion:

dependent(X,L):- module(X), require(X,L1), find(L1,L).

find([],[]).
find([H|T],L):-dependent(H,L2),find(T,L3),append([H|L2],L3,L).

Examples:

?- dependent(se2,L).
L = [se1, oop].

?- dependent(se1,L).
L = [oop].

?- dependent(oop,L).
L = [].