Please, I need help

I’m learning Ocaml at school and I got an exam tomorrow but there is something I still don’t understand. I think my code is correct but the problem come from the type of my function, Ocaml return an error saying : Error: This expression has type char but an expression was expected of type string aiming at (str.[i]). The fact is that I want the first function encadree to understand the © is a character but when I define the function Ocaml return : val encadree : string -> string = instead of : val encadree : char -> string =
Do you know if there is anything to define the type of a var by himself, thank’s !

Here is my code :

let encadree © : string = “[”^c^"]";;

let crochete (str) = let rep = ref “” in
for i = 0 to String.length(str) do
rep:= !rep^encadree(str.[i])

and here is a screen of the exercise if you speak a little of french :slight_smile:

c is a char, and ^ needs two string; you can build a 1-length string with the character c with

String.make 1 c, so your encadree becomes

let encadree (c) : string = “[”^(String.make 1 c)^"]";;

this way the character is transformed to a string, and encadree signature becomes char -> string

(I didn’t check the rest of the code, I don’t understand french; btw please use a meaningful title for the post)

1 Like

Unless the intent of theses exercises is to have you deal with references, you might consider using the Buffer module. In particular Buffer.add_char and Buffer.contents could prove useful.

As a side note, you are using too many parentheses: in OCaml f x y z is equivalent to f(x, y, z) in more common languages. But in OCaml the latter invokes a function whose single argument is a triplet…