Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print graphs nodes 1

Status
Not open for further replies.

curtie7

Programmer
Apr 29, 2010
2
0
0
CZ
I am newie in Prolog, could you please someone help me?
Assignment:
At the entry is given a chart with list of their edges. Write a program in Prolog, which prints a list of all nodes. / / also with inputs for command line

oh (0.1).
oh (0.2).
oh (2.3).
oh (2.4).
oh (3.4).
oh (4.5).

So outputs should be just 0,1,2,3,4,5

Thanks for any advice
 
First of all, I believe your oh predicate takes 2 parameters, like in: oh(0, 1).

With your oh(0.1) it seems as if it takes only 1 parameter, which is a float number.

So, the following algorithm will make clauses like node(0), node(1), node(2), ... appear in your knowledge base. Then, if you want to get all the graph nodes, you should do something like that:

findall(X, node(X), L) ... and L will be a list with all values for X that match the node predicate.

So, here is the entire code:


:- dynamic(node/1).
oh(0, 1).
oh(0, 2).
oh(2, 3).
oh(2, 4).
oh(3, 4).
oh(4, 5).

compute_nodes :-
oh(A, B),
create_node(A),
create_node(B),
fail.

create_node(X) :-
\+node(X),!,
assert(node(X)).
create_node(_).

find_nodes(L) :-
retractall(node(_)),
\+compute_nodes,
findall(X, node(X), L).


compute_nodes always fails because the final 'fail' ... so that means in find_nodes we have to call the negation of compute_nodes so that it actually succeeds
 
ah, to launch everything, just call:

find_nodes(L)

and you will get:

L = [1, 2, 3, 4, 5]
 
Thank you a lot for your answer.

You right, oh predicate takes 2 parameters, like in: oh(0, 1).

I put the code into SWI-Prolog and wanted compile - it wrote:

% Scanning references for 1 possibly undefined predicates


and could you please send me whats is doing?

1) :- dynamic(node/1).
2) \+

Thank you
 
You have help in SWI-Prolog : Help/Online manual... from the editor.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top