Ksunair’s Weblog

Just another WordPress.com weblog

The ‘let’ – continues…

Posted by Unni on August 20, 2008

As I mentioned in the previous post ‘let’ is very important for assignments and funcation definitions. Following things are the ones I learned new today and I thought I share with you all.

Lets look at the following code

  1.  
    1. let a=5;;
    2. a;;
    3. let a=6;;
    4. a;;
    5. let a = a + 1;;
    6. a;;

Atep 2 results in 5 and step 4 result in 6 and step 6 result in 7 as expected. In general we would think the variable is in stack and everytime the value is assigned then stack value is changed. That is correct in c# world but that is not true in F#.  Then how come the value did change in the previous example? Good question and here is the answer. In F# everything is immutable (of course there is exception, we will get to it later). How did the last piece of code worked. As you can imagine, when you assign the first a=5, then F# creates an immutable variable a and assings value 5. When it reaches the next assignment at step 3, it recreates new immutable variable and assignes new value and so on. Hope I am clear.

Here is one more twist that you might like. Lets try the following code

  1.  
    1. let v = 10;;
    2. v;;
    3. let v = 5 in
    4.     v + 2;;
    5. v;;
Important feature of Let

Important feature of Let

When you run the code it procedures result as shown in the above image.  It very important that when you try to see the content of variable ‘v’, you get the value assigned in the first line rather than the result of line 3 and 4. We need to understand this distinction before we delve more deep into the code. It took me some tries to understand exactly what it is.

The first line is a variable assignment and the value is preserved till the value is changed. But when you get line 3 and 4, in this case, ‘let v’ is not an assignment rather than a local funcation (it may not be a correct term). So in line 3 what happens is we are temperoraryly assigning the value 5 for the following expression to execute. What it means is the following statement v + 2 will take the value 5 and evaluate the expression and result it out as the next line in the output. So the value 7 never stored in the variable v.

Let me put it other way. When you attempt an assignment like ‘let v = 10;;’ and excute it at the prompt see what is the F# result, it says, it created a variable of type ‘int’. But when you ran step 3 and 4, it didn’t say it created a variable or funcation instead, it executed the lines and produced a result of 7 thus this value is never preserved.

it is very important that you guys understand I am beginner like you so please send me comments on if I am talking something wrong.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: