The problem is that count_outgoing/2
and count_incoming/2
are implicitly finding your nodes. If you did it explicitly, you'd have the control you need. The simplest thing to do is get a set of nodes and iterate that. Like this:
node(N) :- edge(N, _) ; edge(_, N).
all_nodes(Nodes) :- setof(N, node(N), Nodes).
Your main could then use member/2
to generate the nodes:
count :-
all_nodes(Nodes),
forall(member(N, Nodes),
(count(N, D),
format('node ~a has a difference of ~a~n', [N, D]))).