3

I have started with F# and some code structure wonder me. For example:

I have next code:

let mutable s = 10
s <- 1 + s
printf "%i" s

Everything is clear from math side. I marked "s" as mutable and assigned the new value to "s". Result is 11.

Let me try other part of code:

let mutable s = 10
s = 1 + s
printf "%i" s

This code was worked. But I see that s = 1 + s is a bit strange from the math side. Result of executing of this was 10.

My question, what go on in the last sample? Why didn't I get a error? Is s = 1 + s just ignored? Why? I didn't get any error in the output.

RredCat
  • 5,259
  • 5
  • 60
  • 100

2 Answers2

5

The code

s=1+s

will does not modify s - the equivalent C# is

s==1+s

which just returns false

In fact, the compiler should have issued a warning on that line about a value being unused.

The = operator just does an equality check - it does not assign any values.

John Palmer
  • 25,356
  • 3
  • 48
  • 67
  • You are right. My mistake was that I tried to out the result in the next way "printf "%b" s = 1 + s". And it is crashed. When I modified my code in the next manner "printf "%b" (s = 1 + s)". It works correct and return "false" (as expected) – RredCat Apr 10 '13 at 11:34
  • Moreover, I am using http://www.tryfsharp.org - so it is possible that I have limited output. – RredCat Apr 10 '13 at 11:36
  • 1
    @RredCat I don't get any warning in `fsi` either - they only appear when using the full compiler – John Palmer Apr 10 '13 at 12:35
4

To answer your second question -

Why didn't I get a error? Is s = 1 + s just ignored? Why? I didn't get any error in the output.

The reason for this is that F# distinguishes between top-level scope in a script file and local scope. When you are in a script file (*.fsx file in Visual Studio or inside Try F#), then you probably want to write a sequence of expressions (that may return a value) and execute them interactively one by one. This means that when you write s = 1 + s on one line, this is not an error because you can evaluate the line (select it and hit Ctrl+Enter or Alt+Enter) and see the result in the interactive output.

However, when this appears inside a function (or in other local scope where you are not expected to evaluate it interactively), you get a warning. The easiest way to see is to create a local scope using do (and this works in Try F# too):

do
  let mutable s = 10
  s = 1 + s           // Warning: This expression should have type 'unit', 
                      //    but has type 'bool'. If assigning to a property 
                      //    use the syntax 'obj.Prop <- expr'.
  printf "%i" s
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553