Domanda

Hey im new to prolog and was wondering:

Let's say i have this code:

component(a,b).  
component(a,c).   
component(a,d).  
component(b,e).  
component(b,f).

and i want to create an argument consistsof(X,Y):- component(X,Y); component(Y,Z). that gives me:

Y= b,c,d,e,f

so i want to get the result of "a" plus the result of the ones "a" is connected with (b,e) and (b,f). Im sorry if im not very specific.

È stato utile?

Soluzione

I would use a transitive closure

part_of(Object, Part) :-
    component(Object, C), (Part = C ; part_of(C, Part)).

and setof/3

?- setof(C, part_of(a,C), L).
L = [b, c, d, e, f].

note this requires an acyclic graph, or will loop forever

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top