OCaml syntax error in function

20,123

Solution 1

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.)

Solution 2

Try replacing the semicolon after the let first = true with the keyword in.

Share:
20,123
Admin
Author by

Admin

Updated on October 28, 2020

Comments

  • Admin
    Admin over 3 years

    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.

  • Admin
    Admin over 10 years
    Thank you! I just started using oCaml at my university so I am a total outsider. Is there any way I can execute a block of code only at the first element of the set?
  • Jeffrey Scofield
    Jeffrey Scofield over 10 years
    One way that's good style might be to fold over the set rather than iterating. A fold lets you carry state along (which is what you want to do).
  • Admin
    Admin over 10 years
    I wrote the code using a fold rather the iter, which works how I wanted it to. My only problem is that the exercise I am trying to solve, particularly asks me to use iter. I can solve it with iter, but the I will display it like: {string1,string2,}. I will have an extra comma before the bracket.
  • Jeffrey Scofield
    Jeffrey Scofield over 10 years
    List.iter is an imperative-style construct. So you're more or less forced to use other imperative OCaml features to make it do what you want. You can have mutable state in OCaml: start with let start = ref true. But this just postpones learning how FP actually works (IMHO :-)
  • newacct
    newacct over 10 years
    "This (seemingly minor) point is one of the keys to functional programming." Not necessarily. Scheme is a functional language, and lets you assign to variables. If you're trying to say "purely functional", then OCaml is not a purely functional language anyway. Not being able to assign to variables is just a feature of the ML family of languages.