Lists with Prolog

I am learning rooPlog and have some list problems. I want to return a list of classes that are prerequisites for the specified class. Here is what I still have ...

prereq(262, 221).
prereq(271, 262).
prereq(331, 271).

prerequisites(A, B) :- not(prereq(A, C)).
prerequisites(A, [C|B]) :- prereq(A, C), prerequisites(C, B).

It works, but adds junk to the end.

?- prerequisites(331, A).
A = [271, 262, 221|_G327] ;
false.
+3
source share
1 answer

Maybe you mean

prerequisites(A, []) :- not(prereq(A, _)).
prerequisites(A, [C|B]) :- prereq(A, C), prerequisites(C, B).

You must be sure that there are no loops in your data for this to work ...

+3
source

All Articles