我想否定矩阵的选定元素及其相邻元素。 我的问题是如何在没有“&&”的情况下使这些多个表达式发生。我不太了解语法。 我正进入(状态- 错误:此表达式的类型为 unit 但应为 bool 类型的表达式
let matrix2 =[|[|true;true;false;false|];
[|false;false;true;true|];
[|true;false;true;false|];
[|true;false;false;true|]|];;
let flip_matrix matrix a b=
let n=Array.length matrix in
for i=1 to n do
let n1=Array.length matrix in
for j=1 to n1 do
if i=a && j=b
then
matrix.(i).(j)<- not matrix.(i).(j)&&matrix.(i+1).(j+1)<- not matrix.(i+1).(j+1)&&matrix.(i-1).(j-1)<- not matrix.(i-1).(j-1)
done;
done;
matrix;;
flip_matrix matrix2 1 2;;
答案 0 :(得分:1)
排序运算符 ;
用于将多个表达式链接在一起,
<exp1>; <exp2>
表示先评估<exp1>
,然后评估<exp2>
,例如:
print_endline "Hello!"; print_endline "World."
请注意,;
仅适用于返回类型为 unit
的值的表达式,即仅针对其副作用进行评估并且不会产生任何有用的值。
当您需要链接多个产生有用值的表达式时,您需要将这些值绑定到某些变量,并且必须使用 let <v> = <exp1> in <exp2>
。此表达式将计算 <exp1>
并将其绑定到可用于表达式 <v>
的变量 <exp2>
,然后对其进行计算。示例,
let message = "hello", ^ ", world" in
print_endline message
如您所见,<exp1>; <exp2>
只是一个简写,
let () = <exp1> in <exp2>
另外,注意这可能是一个 let .. in ..
表达式本身,这样你就可以在 OCaml 中链接任意数量的表达式,
let x1 = f1 y1 in
let x2 = f2 y2 in
...
let xN = fN yN in
final_result
现在,我们已准备好使用条件表达式,例如 if
。很自然地假设
if x > 0 then print_endline "Hello"; print_endline "World"
会打印
Hello
World
如果 x
大于零。但这是错误的!正如我最近在此 answer 中描述的那样,if
表达式比 ;
具有更高的优先级(优先级),因此实际上 OCaml 解析器将其拆分为两个表达式:
(if x > 0 then print_endline "Hello"); print_endline "World"
所以最后只有一个表达式处于条件下。与此类优先级问题一样,解决方案是使用括号(或 begin/end
,这是相同的),例如,
if x > 0 then (print_endline "Hello"; print_endline "World")
如果你愿意,你也可以使用更通用的 let .. in ..
,它不需要任何额外的括号,例如,
if x > 0 then
let () = print_endline "Hello" in
print_endline "World"
虽然有点丑:)
答案 1 :(得分:0)
数组的变异赋值计算为单位:
utop # let arr = Array.make 10 0;;
val arr : int array = [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]
utop # arr.(0) <- 1;;
- : unit = ()
在这一行:
matrix.(i).(j)<- not matrix.(i).(j)&&matrix.(i+1).(j+1)<- not matrix.(i+1).(j+1)&&matrix.(i-1).(j-1)<- not matrix.(i-1).(j-1)
您正在使用 &&
将布尔值与 matrix.(i+1).(j+1) <- ...
的计算结果连接起来,后一个表达式作为单位。当然 &&
只适用于连接太布尔值。
答案 2 :(得分:0)
我认为应该这样做:
let matrix33 = [|[|true;true;false;false|];
[|false;false;true;true|];
[|true;false;true;false|];
[|true;false;false;true|]|];;
let flip_matrix matrix a b=
let n=Array.length matrix in
for i=1 to n do
let n1=Array.length matrix in
for j=1 to n1 do
if i=a && j=b
then begin
matrix.(i).(j)<- not matrix.(i).(j);
matrix.(i+1).(j)<- not matrix.(i+1).(j);
matrix.(i).(j+1)<- not matrix.(i).(j+1);
matrix.(i).(j-1)<- not matrix.(i).(j-1);
matrix.(i-1).(j)<- not matrix.(i-1).(j);
end;
done;
done;
matrix;;
flip_matrix matrix33 1 1 ;;```