Ksunair’s Weblog

Just another WordPress.com weblog

Posts Tagged ‘Introduction’

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

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