×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Print graphs nodes

## Print graphs nodes

(OP)
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

### RE: Print graphs nodes

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

### RE: Print graphs nodes

ah, to launch everything, just call:

find_nodes(L)

and you will get:

L = [1, 2, 3, 4, 5]

### RE: Print graphs nodes

[0, 1, 2, 3, 4, 5] ... sorry

### RE: Print graphs nodes

(OP)

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

### RE: Print graphs nodes

You have help in SWI-Prolog : Help/Online manual... from the editor.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!