打印bool矩阵,但用1'和0&0'

发布于 2025-01-25 18:15:54 字数 588 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回首观望 2025-02-01 18:15:54

您的代码已经具有函数,print_s,您必须调用类似的内容,

let example = [|
  [|"1"; "0"|];
  [|"0"; "1" |]
|]

print_s example;;

它将打印出类似的内容

10/n01/n-

,表明您的函数有三个问题:

  1. 无分离的行元素
  2. 缺少新行(应该是“ \ n”不是“/n”
  3. 错误的输入类型,您需要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,

let example = [|
  [|"1"; "0"|];
  [|"0"; "1" |]
|]

print_s example;;

It will print something like

10/n01/n-

This indicates that your function has three problems:

  1. no separation between row elements
  2. missing new line (should be "\n" not "/n")
  3. wrong input type, you want bool array array but it accepts string 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 use if/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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文