Syntax error OCaml

I have a function
let return x = x;
and i need to find a Syntax error in this , but in compiler it doesnt give me any errors at all.

I have few ideas but i dont know if they re correct
First : if it s an unbound value as we use x before declaring it
Second: this one ; shouldnt be here?

Where did you enter this code? In a file? In the REPL? Is this a toplevel function definition? Or is it nested inside some other expression? In any case, most likely it’s the second, there should not be a semi-colon after the binding.

No its a toplevel function definition, it was given exactly like this
I tried it on Try OCaml and even with semi-colon it works fine and doesnt give any errors.

Снимок

OK so you entered in the REPL:

let return x = x; ;;

Which is actually slightly different from your original posted code,

let return x = x;

The reason you didn’t get a syntax error is I guess because the parser ignored the single semi-colon when it saw that the entry was finished with ;;.

Btw you can enter code samples into messages in this forum by using triple-backticks to begin and end the samples. It’s better to do that than to paste images of the code, because images are not very accessible.

No i typed exactly as i wrote
let return x = x;
but it adds the two semi-colons automatically

This is on the OCaml REPL on the command line? It doesn’t do that for me, and actually I’ve never heard of that happening. If I end the entry with just the single semi-colon it just sits there waiting for me to finish entering the ;;.

I use this website to try my code https://try.ocamlpro.com/
But apart from the semi colon this function is absolutely correct , there s nothing wrong with it?

Oh, so Try OCaml works in a slightly different way than the command-line REPL. It has a single-line text box and assumes that the entry is finished as soon as you hit Enter. And so it takes the line, adds the ;; to the end, and sends it off to its version of the REPL, which executes it.

Anyway to answer your question, there is nothing wrong with the function, it’s a fine function :slight_smile:

Okey
thank you very much for your help

1 Like

Indeed, there is nothing wrong apart from the semicolon. Think about why did you wrote it. Your intent was likely to state that your statement was finished (it has this meaning in a lot of languages). BUT in ocaml, single semicolon does not have this meaning at all. I do not know how to explain it clearly, so I provide you example to express this:

let x = () ; (* alone: invalid code*)

let x = () ; 7 (* valid code; literally the same that let x = 7 *)

let x = 4 ; 7 (* invalid code, because expression ending with ";"
                 must be of unit type *)

let x = () ; let y = 7 in y + 3 (* valid code, equivalent to let x = 10 *)

let x = 2 ;
let y = 7  (* invalid code because 2 is not of unit type AND
              let y = 7 should be followed by in <expression>.
              Without the ";", it would have been valid code *)

Be careful because improper use of single semicolon in ocaml leads to error messages very unlikely to help you. (syntax error at a place you would not expect as a beginner).

Welcome and have a good day :slight_smile: