# Using Standard ML's imperative features.

**URL:** <https://boards.straightdope.com/t/using-standard-mls-imperative-features/332706>\
**Category:** Factual Questions\
**Created:** [November 25, 2005, 6:34pm UTC](https://boards.straightdope.com/t/using-standard-mls-imperative-features/332706 "2005-11-25T18:34:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [November 25, 2005, 6:34pm UTC](https://boards.straightdope.com/t/using-standard-mls-imperative-features/332706/1 "2005-11-25T18:34:23Z")

</div>

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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [November 26, 2005, 10:28am UTC](https://boards.straightdope.com/t/using-standard-mls-imperative-features/332706/2 "2005-11-26T10:28:06Z")

</div>

> [@Dominic Mulligan](#):
>
> 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:
> 
> ```auto
> 
> 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?

---

<div class="post-metadata">

**Author:** ![Harmonious\_Discord](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Harmonious\_Discord](https://boards.straightdope.com/u/Harmonious_Discord)\
**Post date:** [November 26, 2005, 11:17am UTC](https://boards.straightdope.com/t/using-standard-mls-imperative-features/332706/3 "2005-11-26T11:17:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [November 26, 2005, 11:33am UTC](https://boards.straightdope.com/t/using-standard-mls-imperative-features/332706/4 "2005-11-26T11:33:52Z")

</div>

> [@](#):
>
> I haven’t writen code in about 10 years, so I not going to try a debug.

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.

---

<div class="post-metadata">

**Author:** ![Harmonious\_Discord](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Harmonious\_Discord](https://boards.straightdope.com/u/Harmonious_Discord)\
**Post date:** [November 27, 2005, 4:33am UTC](https://boards.straightdope.com/t/using-standard-mls-imperative-features/332706/5 "2005-11-27T04:33:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [November 28, 2005, 10:34am UTC](https://boards.straightdope.com/t/using-standard-mls-imperative-features/332706/6 "2005-11-28T10:34:11Z")

</div>

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:

```auto

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.
