为什么 `;;` 在 utop 中给我一个语法错误?
Why is `;;` giving me a syntax error in utop?
我正在做一个小项目,将小程序从 python 转换为 java,反之亦然。
我创建了以下代码,并在 utop 中对其进行了测试。
let c =
let x = "for (int i = 0; i<10; i++)"
and y = "for i in range(0,10):"
in function
| x -> y
| y -> x
| _ -> "Oh no!!";;
出于某种原因,x 和 y 都被认为是未绑定的,但同时任何参数似乎都与 x 匹配。
要使这项工作正常进行,需要按什么顺序编写所有内容?
所以我不完全理解为什么它以这种方式工作,但我至少了解它是如何工作的。
我的问题是,在模式匹配中,与变量的匹配似乎不一定与其值匹配。
简而言之,我应该输入
function
| i when i = x -> y
| i when i = y -> x
| _ -> "Oh no!!";;
如果有人能进一步说明 "x -> y"
与 I when I = x -> y
的区别,我仍然很好奇。
否则,感谢@ghilesZ 向我发送了解决此问题所需的链接!并感谢@BahmanMovaqar 帮助我更好地理解语句的优先级。
只是跟进你的回答。
In pattern-matching, matching to a variable doesn't necessarily seem to match to its value.
这就是为什么它被称为模式匹配而不是值匹配的原因。
顾名思义,模式匹配用于根据模式匹配事物,而不是值。在您在问题中显示的代码中,您实际上并没有将任何内容与 x
或 y
进行比较,而是定义了可以匹配任何内容的名为 x
和 y
的模式。请参阅以下示例:
match 2 with
| x -> "Hey, I matched!"
| _ -> "Oh, I didn't match.";;
- : string = "Hey, I matched!"
Note that this works even if x
was previously defined. In the match
case, the x
from the pattern is actually shadowing the other one.
let x = 42 in
match 1337 with
| x -> Printf.printf "Matched %d\n!" x
| _ -> ();;
Matched 1337!
- : unit = ()
另一方面,模式 i when i = x
实际上与外部变量 x
的值匹配,这就是您的自我回答中的代码有效的原因。但这无论如何都不是模式的目的。
你实际上想做的是不是模式匹配,它是一个简单的条件语句。
let c argument =
let x = "for (int i = 0; i<10; i++)" in
let y = "for i in range(0,10):" in
if argument = x then y
else if argument = y then x
else "Oh no!";;
val c : string -> string = <fun>
它正在运行:
c "for (int i = 0; i<10; i++)";;
- : string = "for i in range(0,10):"
c "for i in range(0,10):";;
- : string = "for (int i = 0; i<10; i++)"
c "whatever";;
- : string = "Oh no!"
此外,不要使用 and
,除非您要定义相互递归的值。
我正在做一个小项目,将小程序从 python 转换为 java,反之亦然。 我创建了以下代码,并在 utop 中对其进行了测试。
let c =
let x = "for (int i = 0; i<10; i++)"
and y = "for i in range(0,10):"
in function
| x -> y
| y -> x
| _ -> "Oh no!!";;
出于某种原因,x 和 y 都被认为是未绑定的,但同时任何参数似乎都与 x 匹配。
要使这项工作正常进行,需要按什么顺序编写所有内容?
所以我不完全理解为什么它以这种方式工作,但我至少了解它是如何工作的。
我的问题是,在模式匹配中,与变量的匹配似乎不一定与其值匹配。
简而言之,我应该输入
function
| i when i = x -> y
| i when i = y -> x
| _ -> "Oh no!!";;
如果有人能进一步说明 "x -> y"
与 I when I = x -> y
的区别,我仍然很好奇。
否则,感谢@ghilesZ 向我发送了解决此问题所需的链接!并感谢@BahmanMovaqar 帮助我更好地理解语句的优先级。
只是跟进你的回答。
In pattern-matching, matching to a variable doesn't necessarily seem to match to its value.
这就是为什么它被称为模式匹配而不是值匹配的原因。
顾名思义,模式匹配用于根据模式匹配事物,而不是值。在您在问题中显示的代码中,您实际上并没有将任何内容与 x
或 y
进行比较,而是定义了可以匹配任何内容的名为 x
和 y
的模式。请参阅以下示例:
match 2 with
| x -> "Hey, I matched!"
| _ -> "Oh, I didn't match.";;
- : string = "Hey, I matched!"
Note that this works even if
x
was previously defined. In the match case, thex
from the pattern is actually shadowing the other one.let x = 42 in match 1337 with | x -> Printf.printf "Matched %d\n!" x | _ -> ();; Matched 1337! - : unit = ()
另一方面,模式 i when i = x
实际上与外部变量 x
的值匹配,这就是您的自我回答中的代码有效的原因。但这无论如何都不是模式的目的。
你实际上想做的是不是模式匹配,它是一个简单的条件语句。
let c argument =
let x = "for (int i = 0; i<10; i++)" in
let y = "for i in range(0,10):" in
if argument = x then y
else if argument = y then x
else "Oh no!";;
val c : string -> string = <fun>
它正在运行:
c "for (int i = 0; i<10; i++)";;
- : string = "for i in range(0,10):"
c "for i in range(0,10):";;
- : string = "for (int i = 0; i<10; i++)"
c "whatever";;
- : string = "Oh no!"
此外,不要使用 and
,除非您要定义相互递归的值。