简体   繁体   中英

Prolog - how to print a list

I need to print list elements but I'm having troubles... I have facts like these:

processor('Intel Core i3 8100', 104, 'low_range')   .
processor('Ryzen 5 2400g', 135, 'middle_range').    
motherboard('Gigabyte Z370P D3', 98, 'low_range')   .
motherboard('Gigabyte GBT AX370M-DS3H', 70, 'middle_range').
.....
configuration(P, M, R, C, A, V, S, D, H, Price_range) :-
    processor(P, _Proc_price, Price_range),
    motherboard(M, _Motherboard_price, Price_range),
    ram(R, _Ram_price, Price_range),
    case(C, _Case_price, Price_range),
    ali(A, _Ali_price, Price_range),
    video_card(V, _Vga_price, Price_range),
    ssd(S, _Ssd_price, Price_range),
    monitor(D, _Monitor_price, Price_range),
    hdd(H, _Hdd_price, Price_range).

I filter these configurations by findall function:

findall(P-M-R-C-A-V-S-D-H, configuration(P, M, R, C, A, V, S, D, H, T), Res),

I need to print Res elements in that way:

configuration number 1
processor
motherboard
ram
case
ali
video card
ssd
monitor
hdd

configuration number 2
processor
motherboard
ram
case
ali
video card
ssd
monitor
hdd

....

configuration number N
processor
motherboard
ram
case
ali
video card
ssd
monitor
hdd

Could you help me please? I'm not understanding how to do that

You may want to use forall/2 instead of findall/3 :

forall(:Cond, :Action) For all alternative bindings of Cond , Action can be proven.

eg:

forall(configuration(P, M, R, C, A, V, S, D, H, T),
    (writeln(configuration), writeln(P), writeln(M), ...)).

Edit: you can make a counter in this way:

:- dynamic(mycounter/1).
:- assertz(mycounter(0)).

incr_mycounter(X):-
    mycounter(X),
    retractall(mycounter(_)),
    succ(X,Y),
    assertz(mycounter(Y)).

and use it like this:

forall(configuration(P, M, R, C, A, V, S, D, H, T),
    (incr_mycounter(N), writeln(configuration-N), writeln(P), writeln(M), ...)).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM