Вопрос

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.

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top