user3614293
user3614293

Reputation: 425

Prolog parsing and output

I have the following predicates in my database, which I successfully parsed from input:

19 ?- listing(element1).
:- dynamic element1/2.

element1('El1', [20]).
element1('El2', [25]).
element1('El3', [30]).

Now, given an appropriate query like 'List all elements' how do I output them nicely, like:

'El1, El2, El3 have values 20, 25, 30 respectively'  ?

Upvotes: 0

Views: 75

Answers (1)

CapelliC
CapelliC

Reputation: 60034

SWI-Prolog supports calling user specified predicates by means of the ~@ format specifier. So, if you write a predicate like

out_comma_sep_list(L) :- atomic_list_concat(L, ' ,', T), write(T).

you can do

report :-
  findall(A-B, element1(A, B), L), pairs_keys_values(L, As, Bs),
  format('~@ have values ~@ respectively', 
      [out_comma_sep_list(As), out_comma_sep_list(Bs)]).

Upvotes: 3

Related Questions