Prolog Beginner: Building a list using Setof Function?

October 29th, 2010

e bus

My Chosen Blogging Software

Click here for Information on Blogging Software

Prolog Beginner: Building a list using Setof Function?

edge(a, b, 3).
edge(b, d, 7).
edge(c, b, 4).
edge(c, f, 11).
edge(a, c, 5).
edge(e, h, 2).
edge(c, e, 4).
edge(d, a, 11).
edge(f, d, 2).
edge(e, g, 8).
edge(g, a, 8).

1: Compose rules for building a list of all nodes mentioned
in some edge. (Hint: consider the “setof” predicate.) (2 pts.)
If we enter the query
?- nodelist(L).
the answer should be
L = [a,b,c,d,e,f,g,h] ;

nodelist(L) :- setof(Y,X^edge(X,Y),L).
Output: L = [a,b,c,d,e,f,g,h] ;
This works for
edge(a, b).
edge(b, d).
edge(c, b).
edge(c, f).
edge(a, c).
edge(e, h).
edge(c, e).
edge(d, a).
edge(f, d).
edge(e, g).
edge(g, a).

Can someone help me with this? I don't know how to build the same list when edge has 3 variables. How do I exclude the numbers from being a part of the list?

Posted in Autoresponders | No Comments »


Leave a Reply

 Comment Form