Ksunair’s Weblog

Just another WordPress.com weblog

Posts Tagged ‘let’

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

Posted in Introduction | Tagged: , , | Leave a Comment »

The ‘let’

Posted by Unni on August 19, 2008

Everything in F# starts with ‘let’ keyword. Variable, value or funcation you name it, it has to start with let. So lets say you want to define an integer of value 5 to a variable ‘a’ then you do it like the following

let a = 5;;

Lets say you want perform an addition of two integer variables 3 and 5 and store the result in variable say ‘b’ then you do so as following

let b = 3 + 5;;

Lets say you want to create a funcation which doubles the input value, you would do so by ‘let’ as follows;

let double x = x + x;;

In here double is the funcation name and x is the input parameter to the funcation.

Anyway, only place (in my very limited knowledge) you do not use let is to execute the command in the console mode. For example if you want to execute the double funcation for the value of 6 then at the F# console, you would do the following

let double x = x + x;;

double 6;;

The second call of double will invoke the double method defined in the previous line and pass value 6 as X and write the answer of 12 on to the console.

Couple of things to remember (for my notes)

  • F# automatically infers the type of a variable by the value assign to it.
  • If you perform an operation with mixed types, the operation will fail.
  • You can force convert the type to another type by specifying the anticipated type infront before the call.

Posted in Introduction | Tagged: , , | Leave a Comment »