Reputation: 23
I want recursively store a family relation in a list structure. I have the following code:
descendantTree(X,[X]) :-
\+ parent(X,_).
descendantTree(X,L) :-
forall(parent(X,Y), descendantTree(Y,YL)),
append([X],[YL],L).
parent(john, paul).
parent(john, mary).
parent(paul, henry).
parent(paul, june).
parent(mary, adam).
parent(henry, helen).
What I expected is like:
L =[john,[paul,[henry,[helen]],[june]],[mary,[adam]]]
But actually it just return:
L = [john, _9250].
Could you tell me why?
Upvotes: 2
Views: 102
Reputation: 18683
The forall/2
predicate implements a generate and test loop using negation. It can be defined as:
% for all solutions of Generate, Test is true
forall(Generate, Test) :-
\+ (Generate, \+ Test)).
The use of negation means that no bindings will be returned when a call succeeds. In your case, that means that you're calling:
append([X],[_],L)
hence the result you get:
L = [john, _9250].
Look instead into the all-solutions family of predicates: bagof/3
, setof/3
, and findall/3
and then consider editing your question with your findings.
Upvotes: 1