序言。如何打印编码矩阵?
Prolog. How to print encoded a matrix?
我有下一个矩阵:
map(1,[[1,0,0,0,0,0,0,0,0,0],
[1,1,0,0,1,1,0,0,0,0],
[0,1,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,1,0,0,0],
[0,1,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]]).
我想像这样打印这个矩阵:
-如果矩阵元素值为0 -> print('~')
-如果矩阵元素值为1 -> print('#').
我尝试这样做,但我的方法每次都打印错误。这是我的代码:
print_encoded([H|T]) :-
H==0 ->
write('~');
H==1 ->
write('#');
print_encoded(T).
showEncoded :-
map(_,Map),
print_encoded(Map).
也许这是一个简单的问题,但 prolog 对我来说是一种新的编程语言。预先感谢您的帮助。
对于 print_encoded,您没有 [] 的基本案例。
可以是
print_encoded([]) :-
nl.
您可以使用 "functional spirit" 和 SWI-Prolog 的模块 lambda
:- use_module(library(lambda)).
print_encoded(M) :-
maplist(\X^(maplist(\Y^(Y = 0
-> write('~')
; write('#')),
X),
nl),
M).
maplist 似乎是为此类任务而构建的...
encode_map(M) :- maplist(maplist(encode_cell), M).
encode_cell(0) :- write('~').
encode_cell(1) :- write('#').
我有下一个矩阵:
map(1,[[1,0,0,0,0,0,0,0,0,0],
[1,1,0,0,1,1,0,0,0,0],
[0,1,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,1,0,0,0],
[0,1,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]]).
我想像这样打印这个矩阵:
-如果矩阵元素值为0 -> print('~')
-如果矩阵元素值为1 -> print('#').
我尝试这样做,但我的方法每次都打印错误。这是我的代码:
print_encoded([H|T]) :-
H==0 ->
write('~');
H==1 ->
write('#');
print_encoded(T).
showEncoded :-
map(_,Map),
print_encoded(Map).
也许这是一个简单的问题,但 prolog 对我来说是一种新的编程语言。预先感谢您的帮助。
对于 print_encoded,您没有 [] 的基本案例。 可以是
print_encoded([]) :-
nl.
您可以使用 "functional spirit" 和 SWI-Prolog 的模块 lambda
:- use_module(library(lambda)).
print_encoded(M) :-
maplist(\X^(maplist(\Y^(Y = 0
-> write('~')
; write('#')),
X),
nl),
M).
maplist 似乎是为此类任务而构建的...
encode_map(M) :- maplist(maplist(encode_cell), M).
encode_cell(0) :- write('~').
encode_cell(1) :- write('#').