打印bool矩阵,但用1'和0&0'
我正在执行一项任务,其中我需要拥有一个函数printMat:bool数组数组 - >单位=< fun>
那 给定一个布尔矩阵,它用屏幕和零将其打印,而不是 真假。
到目前为止,我仅设法用1和0打印已经存在的矩阵,而我的第一个变量是字符串而不是bool。我只是想知道如何更改代码,因此我需要调用函数并输入Bool数组数组而不是将声明为一个?
let matrix = [|
[|true; true; false; false|];
[|false; false; true; true|];
[|true; false; true; false|];
[|true; false; false; true|]
|];;
let print_s matrix =
let n = Array.length matrix in
for i = 0 to n - 1 do
let n1 = Array.length matrix in
for j = 0 to n1 - 1 do
print_string matrix.(i).(j);
done;
print_string "/n";
done;;
I'm doing a task where I need to have a function printmat : bool array array -> unit = <fun>
that
given a bool matrix it prints it on screen with ones and zeros, instead of
true and false.
So far, I only managed to print the already existing matrix with 1's and 0's and my first variable is a string instead of bool. I was just wondering how to change to code so I need to CALL the function and type in the bool array array instead of having the declared one?
let matrix = [|
[|true; true; false; false|];
[|false; false; true; true|];
[|true; false; true; false|];
[|true; false; false; true|]
|];;
let print_s matrix =
let n = Array.length matrix in
for i = 0 to n - 1 do
let n1 = Array.length matrix in
for j = 0 to n1 - 1 do
print_string matrix.(i).(j);
done;
print_string "/n";
done;;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码已经具有函数,
print_s
,您必须调用类似的内容,它将打印出类似的内容
,表明您的函数有三个问题:
“ \ n”
不是“/n”
)bool数组阵列
,但它接受字符串>字符串阵列阵列< /code>
要将布尔值打印为一个数字,您可以编写助手功能并使用它代替
print_string
(显然接受字符串)。您可以使用如果/then/then/else
来区分布尔变量的两个可能状态。因此,如果布尔值为真,则应打印“ 1”
否则您应该打印“ 0”
。You code already has a function,
print_s
, which you have to call something like this,It will print something like
This indicates that your function has three problems:
"\n"
not"/n"
)bool array array
but it acceptsstring array array
To print a boolean value as a number, you can write a helper function and use it instead of
print_string
(which obviously accepts a string). You can useif/then/else
to distinguish between two possible states of a bool variable. So if the boolean value is true, then you should print"1"
else you should print"0"
.