Prolog Beginner: Building a list using Setof Function?

Filed under: building a list — admin @ 5:33 am

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?

Let me guess its for 311
do your own work or i will fail you
hahahahhahh

powered by Yahoo Answers

1 Comment »

  1. Let me guess its for 311
    do your own work or i will fail you
    hahahahhahh
    References :

    Comment by Darkassassin — August 16, 2010 @ 10:59 am

RSS feed for comments on this post. TrackBack URL

Leave a comment