OCaml delimiters and scopes

Posted by Jack on Stack Overflow See other posts from Stack Overflow or by Jack
Published on 2010-06-13T16:00:26Z Indexed on 2010/06/13 16:02 UTC
Read the original article Hit count: 209

Filed under:
|
|

Hello!

I'm learning OCaml and although I have years of experience with imperative programming languages (C, C++, Java) I'm getting some problems with delimiters between declarations or expressions in OCaml syntax.

Basically I understood that I have to use ; to concatenate expressions and the value returned by the sequence will be the one of last expression used, so for example if I have

exp1; exp2; exp3

it will be considered as an expression that returns the value of exp3. Starting from this I could use

let t = something in exp1; exp2; exp3

and it should be ok, right?

When am I supposed to use the double semicol ;;? What does it exactly mean?

Are there other delimiters that I must use to avoid syntax errors?

I'll give you an example:

let rec satisfy dtmc state pformula = 
  match (state, pformula) with
    (state, `Next sformula) ->
        let s = satisfy_each dtmc sformula
        and adder a state = 
            let p = 0.; 
            for i = 0 to dtmc.matrix.rows do
                    p <- p +. get dtmc.matrix i state.index
            done;
            a +. p
        in
            List.fold_left adder 0. s
      | _ -> []

It gives me syntax error on | but I don't get why.. what am I missing? This is a problem that occurs often and I have to try many different solutions until it suddently works :/

A side question: declaring with let instead that let .. in will define a var binding that lasts whenever after it has been defined?

What I basically ask is: what are the delimiters I have to use and when I have to use them. In addition are there differences I should consider while using the interpreter ocaml instead that the compiler ocamlc?

Thanks in advance!

© Stack Overflow or respective owner

Related posts about syntax

Related posts about ocaml