Hi, everyone!

I am learning OCaml and I have a doubt. Hope to get help on it

The following expression gives a boolean value true as output. Can anyone please explain me what is happening here?

{ | " \ \ " | } = " \ " \ \ \ \ \ " " ; ;

Thanks

Hi, everyone!

I am learning OCaml and I have a doubt. Hope to get help on it

The following expression gives a boolean value true as output. Can anyone please explain me what is happening here?

{ | " \ \ " | } = " \ " \ \ \ \ \ " " ; ;

Thanks

OCaml has two notations for string constants. Your code is using the two notations for the same string, and is comparing them to see if they are equal. The two strings are the same, so the result is true.

You can find a detailed explanation of the string constants in Section 7.1 of the OCaml manual.

A quick synopsis: the notation at the left uses `{| ... |}`

to enclose arbitrary characters that do not need to be escaped. The notation at the right uses `" ... "`

to enclose characters (as in many languages), but `"`

and `\`

need to be escaped with `\`

.

Whoever came up with your example has chosen pretty much the worst case string for the right-hand (previously the only) kind of string constant It illustrates why the left-hand (newer) kind of string constant was added.

1 Like

Oh ok, got it!

Thank you so much for clearing my doubt.

@shubhi2000, did you get confused by the presentation of strings in the manual? I have been wondering if there is too much information presented too soon in this case.

Actually yes. In the first chapter where the strings are introduced, the examples given are not explained so I would suggest that either they should be explained in the first chapter or those examples should be moved to Chapter 7 where the escape sequences are explained.