我尝试在屏幕上打印列表元素,但是以自定义方式打印。 当我编译代码时,出现这样: print
我的代码:
printTray([L0,L1,L2,L3,L4,L5,L6,L7,L8,L9,L10]) :-
nl,
printLine(L0),
print('---------+---+---+---+---+---+---+---+---+---'), nl,
printLine(L1),
print('---------+---+---+---+---+---+---+---+---+---'), nl,
printLine(L2),
print('---------+---+---+---+---+---+---+---+---+---'), nl,
printLine(L3),
print('---------+---+---+---+---+---+---+---+---+---'), nl,
printLine(L4),
print('---------+---+---+---+---+---+---+---+---+---'), nl,
printLine(L5),
print('---------+---+---+---+---+---+---+---+---+---'), nl,
printLine(L6),
print('---------+---+---+---+---+---+---+---+---+---'), nl,
printLine(L7),
print('---------+---+---+---+---+---+---+---+---+---'), nl,
printLine(L8),
print('---------+---+---+---+---+---+---+---+---+---'), nl,
printLine(L9),
print('---------+---+---+---+---+---+---+---+---+---'), nl,
printLine(L10),
print('---------+---+---+---+---+---+---+---+---+---'), nl.
printLine([P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10]) :-
write(P0),
print(' '),
write(P1),
print(' | '),
write(P2),
print(' | '),
write(P3),
print(' | '),
write(P4),
print(' | '),
write(P5),
print(' | '),
write(P6),
print(' | '),
write(P7),
print(' | '),
write(P8),
print(' | '),
write(P9),
print(' | '),
write(P10),
print(' '),
nl.
printTray子句中的列表是托盘的行。因此,对于每一行,我逐个打印元素。 我不知道这个的原因,我尝试了另一种方法但结果却是一样的。 当我更改变量值的列表元素的值时会发生问题。
答案 0 :(得分:0)
也许您只需要使用像
这样的实用程序替换所有write / 1vwrite(V) :- var(V) -> write(' ') ; write(V).
对代码进行一些重构会导致
printTray(Tray) :-
nl, maplist(printLine, Tray).
printLine([P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10]) :-
vwrite(P0),
write(' '),
maplist([P]>>(vwrite(P),write(' | ')), [P1,P2,P3,P4,P5,P6,P7,P8,P9]),
vwrite(P10),
writeln(' '),
writeln('---------+---+---+---+---+---+---+---+---+---').
vwrite(V) :- var(V) -> write(' ') ; write(V).
与此测试数据配对
tray([
[_,_,_,_,_,_,_,_,_,_,_],
[_,_,_,_,_,_,_,_,_,_,_],
[y,_,_,_,_,_,_,_,_,_,_],
[_,_,_,_,_,_,_,_,_,_,_],
[_,_,_,_,_,_,_,_,_,_,_],
[_,_,_,_,_,_,_,_,_,_,_],
[_,_,_,_,_,_,_,_,1,_,_],
[_,_,_,_,_,_,_,_,_,2,_],
[_,_,_,_,_,_,_,_,_,_,3],
[_,_,_,_,_,_,_,_,_,_,_],
[_,_,_,_,_,_,_,_,_,_,_]]).
产量
?- tray(T), printTray(T).
| | | | | | | | |
---------+---+---+---+---+---+---+---+---+---
| | | | | | | | |
---------+---+---+---+---+---+---+---+---+---
y | | | | | | | | |
---------+---+---+---+---+---+---+---+---+---
| | | | | | | | |
---------+---+---+---+---+---+---+---+---+---
| | | | | | | | |
---------+---+---+---+---+---+---+---+---+---
| | | | | | | | |
---------+---+---+---+---+---+---+---+---+---
| | | | | | | 1 | |
---------+---+---+---+---+---+---+---+---+---
| | | | | | | | 2 |
---------+---+---+---+---+---+---+---+---+---
| | | | | | | | | 3
---------+---+---+---+---+---+---+---+---+---
| | | | | | | | |
---------+---+---+---+---+---+---+---+---+---
| | | | | | | | |
---------+---+---+---+---+---+---+---+---+---