Types 如何修复此语法错误并改进代码?
我正在做一些游戏的模拟滑动拼图(见游戏说明)。事情是这样的:Types 如何修复此语法错误并改进代码?,types,ocaml,Types,Ocaml,我正在做一些游戏的模拟滑动拼图(见游戏说明)。事情是这样的: 首先我做了一个新的类型: 行和列表示空磁贴的坐标,磁贴是一个难题 然后我做了一个打印拼图的功能,以便更好地解释: 现在我想做一个函数,在拼图中滑动单元格。这应该是一个以一个谜题和两个整数为参数的函数。整数表示要滑动到空磁贴的单元格的位置。输出应该改变。但是,如果位置与空磁贴不相邻或位置超出边界,则应返回未更改的拼图 我的尝试: let slide tile i j= if (i=tile.row+1 &&
- 首先我做了一个新的类型:
行
和列
表示空磁贴的坐标,磁贴
是一个难题
- 然后我做了一个打印拼图的功能,以便更好地解释:
- 现在我想做一个函数,在拼图中滑动单元格。这应该是一个以一个谜题和两个整数为参数的函数。整数表示要滑动到空磁贴的单元格的位置。输出应该改变。但是,如果位置与空磁贴不相邻或位置超出边界,则应返回未更改的拼图
let slide tile i j=
if (i=tile.row+1 && j=tile.column) || (i=tile.row-1 && j=tile.column) || (i=tile.row && j=tile.column+1) || (i=tile.row-1 && j=tile.column-1) then
tile.row<-i; tile.column<-j; tile;
else tile;;
(一个拼图的例子:let puzzle 1={mat=[|[|'1';'2';'3'|][|'4';'5';'6'|][|'7';'8';'9'|][124;行=3;列=3};
)
如何解决此问题?问题在尾部
在then
分支中,下面是最小化的示例(在名为OCaml
的OCaml顶级中运行)
正确的语法是
# if true then print_endline "truth is true" else print_endline "oops";;
truth is true
- : unit = ()
但这还不是全部。如果要将多个表达式连接到代码>在任一分支中,如果
分支必须用括号分隔(要么(
和)
要么开始
和结束
,它们的作用相同),例如
这里的问题是if
表达式的优先级高于代码>将其拆分为三个表达式:
如果为真,则打印“真为真”
print\u endline“我说!”
else打印\u结尾行“oops”
最后一部分显然不是一个语法上有效的表达
正确的语法应该是
# if true then (print_endline "truth is true"; print_endline "I say!") else print_endline "oops";;
truth is true
I say!
既然我们在这里,我想提醒你关于OCaml的警告。想象一下下面的表达式
if false then print_endline "you will never"; print_endline "see this!"
你可能认为它不会打印任何东西,但是
# if false then print_endline "you will never"; print_endline "see this!";;
see this!
- : unit = ()
这也是因为if
绑定比排序操作符更紧代码>。所以我们有两种说法,
1. `if false then print_endline "you will never"`;
2. `print_endline "see this!"`
The first one is predicated and will never print anything, but the second is unconditional.
To summarize, if you have `;` inside your `if` expressions, always delimit them with parentheses (I prefer `begin/end`). Even better, try to employ functional style, so that you don't even need to ever use `;` in your code. OCaml is functional language and works much better with pure code.
[1]: https://ocaml.org/manual/expr.html#ss:precedence-and-associativity
The root of the problem is that `;` breaks the `if` expression is it has hi
您应该使用开始…结束
或括号将子句中的句子组合在一起:。。。然后(tile.rowYeah,就是这样,谢谢。但是当我调用幻灯片拼图12;
时,我得到了错误的输出。拼图应该是不变的,但事实并非如此。if语句中的条件似乎是错误的?老实说,一开始我根本没有看逻辑,只是解决了与语法相关的问题。看着幻灯片,我认为最后一个问题中的条件如果
是错误的-您减少了行和列,我猜这看起来像是“对角线移动”,不应该允许。因此,使用您的拼图1定义幻灯片拼图1 2
进行(非法)移动。这是您正在寻找的问题吗?另一件事-您的幻灯片功能只是更改了“洞”位置,但不会像滑动拼图中预期的那样交换“tile”-是有意的吗?您可能会发现类似i=tile.row+1&&j=tile.column的内容更容易阅读,如果编写为:(i,j)=(tile.row+1,tile.column)则更容易解释
?为了补充这一点,根据问题,格式化代码通常是发现这些类型问题的一种非常有用的方法。问题表明没有缩进,运算符周围的空间使用得非常糟糕。值得一读。更重要的是,最好使用现有的ide自动缩进、完成和类型检查ng,例如Emacs或VsCode
# if true then print_endline "truth is true"; print_endline "I say!" else print_endline "oops";;
Line 1, characters 67-71:
1 | if true then print_endline "truth is true"; print_endline "I say!" else print_endline "oops";;
^^^^
Error: Syntax error
# if true then (print_endline "truth is true"; print_endline "I say!") else print_endline "oops";;
truth is true
I say!
if false then print_endline "you will never"; print_endline "see this!"
# if false then print_endline "you will never"; print_endline "see this!";;
see this!
- : unit = ()
1. `if false then print_endline "you will never"`;
2. `print_endline "see this!"`
The first one is predicated and will never print anything, but the second is unconditional.
To summarize, if you have `;` inside your `if` expressions, always delimit them with parentheses (I prefer `begin/end`). Even better, try to employ functional style, so that you don't even need to ever use `;` in your code. OCaml is functional language and works much better with pure code.
[1]: https://ocaml.org/manual/expr.html#ss:precedence-and-associativity
The root of the problem is that `;` breaks the `if` expression is it has hi