2011-12-17 48 views
-1

我在序言是新的,我想事實打印初始和目標其實 像下面序言打印像目標或初始

initial([8,1,3,7,0,2,6,5,4]). 

goal([1,2,3,8,0,4,7,6,5]). 

============= 

= 8 = 1 = 3 = 

============= 

= 7 = 0 = 2 = 

============= 

= 6 = 5 = 4 = 

============= 

你能幫助我做到這一點?

回答

1

簡單的硬編碼方式:

pretty_print([A, B, C, D, E, F, G, H, I]) :- 
    write(=============), nl, nl, 
    write('= '), write(A), write(' = '), write(B), write(' = '), write(C), write(' ='), nl, nl, 
    write(=============), nl, nl, 
    write('= '), write(D), write(' = '), write(E), write(' = '), write(F), write(' ='), nl, nl, 
    write(=============), nl, nl, 
    write('= '), write(G), write(' = '), write(H), write(' = '), write(I), write(' ='), nl, nl, 
    write(=============), nl, nl. 

與格式:

pretty_print(L) :- 
    format('=============~n 
= ~w = ~w = ~w =~n 
=============~n 
= ~w = ~w = ~w =~n 
=============~n 
= ~w = ~w = ~w =~n 
=============', L). 

一些functionnal幫助:

length_(A, B) :- length(B, A). 
pretty_print(L) :- 
    length(Rows, 3), 
    maplist(length_(3), Rows), 
    append(Rows, L), 
    maplist(format('=============~n~n= ~w = ~w = ~w =~n~n'), Rows), 
    write(=============), nl. 

lambda模塊:

pretty_print(L) :- 
    length(Rows, 3), 
    maplist(\X^length(X, 3), Rows), 
    append(Rows, L), 
    maplist(format('=============~n~n= ~w = ~w = ~w =~n~n'), Rows), 
    write(=============), nl. 

用法:

?- initial(X), pretty_print(X). 

?- goal(X), pretty_print(X).