I have to create a function which will display each element from a set of strings. I did the following:
module S = Set.Make(String);;
module P = Pervasives;;
let write x = (
P.print_string("{"); let first = true;
S.iter (fun str -> (if first then () else P.print_string(","); P.print_string(str))) x;
P.print_string("}");
P.print_newline
);;
^
At the end of the program (where I placed that sign) it appears I have an error: Syntax error: operator expected. Please help me solve this.
I believe your syntactic problem is with let
. Except in top-level code (outermost level of a module), let
must be followed by in
.
There are many other problems with this code, but maybe this will let you find the next problem :-)
A few notes:
Variables in OCaml are immutable. So your variable named first
will always be true. You can't change it. This (seemingly minor) point is one of the keys to functional programming.
You don't need to reference the Pervasives
module by name. That's why it's called "pervasive". You can just say print_string
by itself.
Your last call to print_newline
isn't a call. This expression just evaluates to the function itself. (You need to give it an argument if you want to call the function.)
Try replacing the semicolon after the let first = true
with the keyword in
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With