I can't tell the difference between; and; in OCaml.

Asked 2 years ago, Updated 2 years ago, 101 views

When you write the following code with the OCaml code in the .ml file,

let print_for_print_teststr=
  for i = 0 to String.length - 1 str do
    print_string(str)
    print_newline()
  done;;

The third line received an error message indicating that ; is missing.I actually supplemented ; and it worked, but I'm not sure what the difference is between ; and in OCaml.If you don't mind, please let me know.

ocaml

2022-09-30 15:40

2 Answers

Here (ocaml.org) will be helpful.

Here are some excerpts:

Rule 1.;; When to use is to separate the sentences at the top of the code.No need for function definitions or other statements.

Rules 3 and 4 are about a single ;This is a completely different thing from ;;.One semicolon; is supposed to be a sequence point.In other words, this is exactly the same thing as the C, C++, Java and Perl semicolons.

For the third line only, please refer to: The fourth line is the end of the block, so you can see the end of the expression without ;. This is not the case in the third line, so you need ; to indicate the break in the expression.


2022-09-30 15:40

Let's take a look at the specifications.

http://caml.inria.fr/pub/docs/manual-ocaml-400/language.html

http://caml.inria.fr/pub/docs/manual-ocaml-400/manual020.html

An excerpt of Compilation units and specification, definition is defined as follows:

 unit-interface::={specification[;]} 
unit-implementation::={definition[;]}    

specification::=value-name:typexpr  
    ∣    external value-name —typexpr=external-declaration  
    ∣    type-definition  
    ∣    exception constr-decl  
    ∣    class-specification  
    ∣    clastype-definition  
    ∣    module module-name —Module-type  
    ∣    module module-name {(module-name:module-type)}—module-type  
    ∣    module type modtype-name  
    ∣    module type modtype-name = module-type  
    ∣    open module-path  
    ∣    include module-type  

definition::=let[rec]let-binding {andlet-binding}  
    ∣    external value-name —typexpr=external-declaration  
    ∣    type-definition  
    ∣    exception-definition  
    ∣    class-definition  
    ∣    clastype-definition  
    ∣    module module-name {(module-name:module-type)}[:module-type] = module-expr  
    ∣    module type modtype-name = module-type  
    ∣    open module-path  
    ∣    include module-expr

OCaml has three expressions: definition, specification (collectively called sentences), and expression. It would be good to remember that; is used to separate sentences and; is used to describe expressions continuously.;; is optional, but can be used at the top level or when needed.

let a=1 is defined and
vala:int is the specification and
Let b = 1 in b is an expression.

let a=();();();();1;;

defines a as 1.
You can also write formulas as sentences, so arrange formulas at the top level

1;;
2;;
letb = 1 in b;;
3

You can also write like this.
You must write ;; if you want to write expressions as sentences continuously.

f
1

In OCaml, line breaks are meaningless, so you can apply argument 1 to the f function.


2022-09-30 15:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.