Using Standard ML's imperative features.

I’ve asked a few ML related questions before and got some good responses, so I’ll ask this one here. I’ve already searched Google, fiddled with my code for ages and read my reference book (ML for the Working Programmer by Paulson) and I’m still stumped.

I’m trying to use a reference to an integer and update the value of that integer in a series of if statements, for example:



fun example() =
  let
    val a = ref 0
  in
    if expression1 = true then
      a := !a + 3
    if expression2 = true then
      a := !a + 5
    !a
  end


I can get references to in simple examples using the interactive environment but when using this code in a file, the compiler balks. In my example, the first if statement will be parsed correctly, but then the compiler will come back with a syntax error on the second if statement. Now, I know that when evaluating some expressions for their side effects, you must use a semicolon at the end of the expression, but no combination of parentheses, semicolons etc. will get the compiler to accept my file.

Can someone therefore explain how to use references correctly in a .sml file, please?

Thanks in advance.

BUMP.

Nobody has any idea?

I haven’t writen code in about 10 years, so I not going to try a debug. Besides I have to recover someone’s messed up main drive this morning. I don’t want to overload before I work on their problem.

You need to work on the timing of posts. You should have waited about 3 more hours to get most of the morning posters that are starting to drag in their sorry butts. The holidays are not the best times to ask a serious question. Add in the deer hunting and bargin shoppers the day after Thanksgiving.

Good luck.

To clarify, I’m not asking for people to debug my code, I’m asking about a specific feature of SML’s grammar.

I realise I should have waited to bump the post, but I’m in the lab early this morning working on something else, I got bored and wanted to try to finish the program that has the problem mentioned in the OP.

The debug thing was bad wording. (Now I can post and it’s back on top, since I had to tell you this.)

OK, I’ve fixed it. On the off chance that anyone here should be having the same problem (a small probability, I guess), Standard ML expects all if statements to be followed by an else. The reworked function, therefore, is:



fun test() =
  let
    val a = ref 0
  in
    if expression1 = true then
      a := !a + 2
    else
      ();
    if expression2 = true then
      a := !a + 2
    else
      ();
    !a
  end


Why this is the case I have no idea.