How can I combine format and print in prolog?

How can I combine format and print in prolog?

Basically, I pretend to print a message like "Element 5 occurs 3 times" in prolog. Number 5 and 3 depends of the execution.

In Python, it would be like:

print("Element %d occurs %d times" % (element, occurrences))

For example, in a small example like:

count_occur(X, [], N) :- write("Element ??X occurs ??N times.").

Upvotes: 2

Views: 152

Answers (1)

coder
coder

Reputation: 12972

Use format/2 like:

count_occur(X, [], N) :-  format('Element ~d occurs ~d times ~n',[X, N]).

Example:

?- count_occur(2,[],3).
Element 2 occurs 3 times
true.

Upvotes: 3

Related Questions