简体   繁体   English

如何在Ocaml中正确使用分隔符

[英]How to correctly use separators in Ocaml

Consider the following code in OCamel: 考虑一下OCamel中的以下代码:

let rodrigue="The cid" ; let diegue="cid's father"

this gives a syntax error on tryOcaml. 这给出了tryOcaml的语法错误。 What is wrong? 怎么了? It seems to work when I removed the ; 当我删除;时,它似乎起作用; Please help. 请帮忙。

Also, rodrigue.[4] 另外,罗德里格。[4] <- 'c' gives unknown constructor error. <-'c'给出未知的构造函数错误。 Does this mean that we can't use <- ? 这是否意味着我们不能使用<-? as rodrigue.[4] 作为僵硬。[4] is immutable or there is a function to change a character in a string? 是不可变的,还是有功能来更改字符串中的字符?

What does ; 有什么作用; do ? 怎么办? I tried 4=4 ;6=5 , it gave a value of bool= false. 我尝试了4=4 ;6=5 ,它给出了bool = false的值。

is 4=4 an expression or a statement? 4=4是表达式还是语句?

what about let x=5 ? let x=5怎么样?

So let x=8 let y=9 executes both statements but $4=4 ; 因此, let x=8 let y=9执行两个语句,但$ 4 = 4; 5=6$ only evaluate the last expression? 5 = 6 $只计算最后一个表达式?

What is the difference between an expression and a statement? 表达式和语句之间有什么区别?

What is toplevel? 什么是顶层? What is the difference between toplevel and normal OCamel expression? 顶级和正常OCamel表达之间有什么区别?

There are two places where you can use let in OCaml. 在OCaml中可以在两个地方使用let At the top level of a module you can have this: 在模块的顶层,您可以执行以下操作:

let pattern = expression

A module is, in essence, a collection of named values, and this form of let is the way of defining a name and its value. 本质上,模块是命名值的集合,而let这种形式就是定义名称及其值的方式。

On the other hand, there is an OCaml expression that looks like this: 另一方面,有一个OCaml表达式,如下所示:

let pattern = expression1 in expression2

This is, in essence, a way of defining local variables for use in expression2 . 本质上,这是定义要在expression2使用的局部变量的expression2

Your code with the ; 您的代码带有; doesn't work as either of these forms. 不能同时使用这两种形式。 A ; A ; is used to combine two expressions into a single expression. 用于将两个表达式组合为一个表达式。 But let pattern = value is not an expression. 但是let pattern = value不是表达式。 It has the form of a top-level declaration of a module. 它具有模块的顶级声明的形式。

The reason it works when you remove the ; 删除;时起作用的原因; is that you then have two top-level declarations. 那么您就有两个顶级声明。 That will work as long as the code appears at the top level of a module. 只要代码出现在模块的顶层,就可以使用。 In this case, it defines the names rodrigue and diegue as named values of the module. 在这种情况下,它定义了名称rodriguediegue作为模块的命名值。

Other answers: 其他答案:

In recent versions of OCaml, strings are not mutable. 在最新版本的OCaml中,字符串是不可变的。 You need to use the bytes type if you need mutability. 如果需要可变性,则需要使用bytes类型。 As part of this change the string.[...] <- ... notation became obsolete. 作为此更改的一部分, string.[...] <- ...标记已过时。 You should use Bytes.set instead. 您应该改用Bytes.set

# let mystring = "abc";;
val mystring : string = "abc"
# let mybytes = Bytes.of_string "abc";;
val mybytes : bytes = Bytes.of_string "abc"
# mystring.[0] <- 'b';;
Warning 3: deprecated: String.set
Use Bytes.set instead.
Error: This expression has type string but an expression was 
            expected of type bytes
# Bytes.set mybytes 0 'b';;
- : unit = ()
# mybytes;;
- : bytes = Bytes.of_string "bbc"

OCaml doesn't really have statements. OCaml确实没有声明。 In places where you would have a statement in some other languages, you have an expression of type unit . 在您可能会要求使用其他某些语言的语句的地方,您会有一个类型为unit的表达式。 For example, the for statement in OCaml is actually an expression with unit type. 例如,OCaml中的for语句实际上是具有unit类型的表达式。

# for x = 1 to 1 do () done = ();;
- : bool = true

From this point of view, then, 4 = 4 is an expression because it has type bool (and not unit ). 从这个角度来看,则4 = 4是一个表达式,因为它的类型为bool (而不是unit )。

So then let x = 5 is neither an expression nor a statement (which is just a kind of expression). 因此, let x = 5既不是表达式也不是语句(这只是一种表达式)。 As I pointed out above, this has the form of a top-level module declaration. 正如我在上面指出的那样,它具有顶级模块声明的形式。 Hence, it can only appear at the top of a module. 因此,它只能出现在模块的顶部。

In contrast, let x = 5 in x + 2 is an expression with the value 7. 相反, let x = 5 in x + 2中的let x = 5 in x + 2是值为7的表达式。

The name toplevel is what OCaml veterans call the read-eval-print loop of OCaml. toplevel名称是OCaml退伍军人所说的OCaml的read-eval-print循环。 In other words, it's a name for the OCaml interpreter. 换句话说,它是OCaml解释器的名称。

In my answers above I'm using "top level" to mean the outermost syntactic nesting level of a module. 在上面的回答中,我使用“顶层”来表示模块的最外层语法嵌套层。 It's not the same as the toplevel (the OCaml interpreter). 它与顶层(OCaml解释器)不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM