F Sharp for beginners: Modules in F #

List of articles of F Sharp beginner series

All the codes you write in F Sharp are by default in a MODULE, regardless of the fact that whether you explicitly define a module or not.

Anonymous Module

clip_image001

Anonymous module has the same name as of file name with first letter in upper case.

Let us say,

  1. you are writing codes inside a file called myfile.fs
  2. You have explicitly not given a module name.
  3. Inside the file myfile.fs, you have code as below

 

clip_image002

4. By default module for above code would be Myfile (same as the file name of the code with first letter in upper case)

image

Creating Module

Explicitly Module name can be given by using the keyword “Module” .

clip_image004

Now if you want to access variable myNumber in some other module then you will access as below,

clip_image005

Creating Nested Modules

You can create nested modules by using equal to (=) sign with the module name.

clip_image006

In above code we have defined nested modules. You can access myNumber1 variable as below,

clip_image008

In other module you will find inellisense also working,

clip_image009

This was all about Modules in F sharp.

I hope this post was useful. Thanks for reading Smile

List of articles of F Sharp beginner series

 

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

FSharp for beginners: what is List.map function?

List.map function creates a new collection by applying a function to the given collection.

clip_image001

Just have a look on below example,

clip_image002

When you print r1 you should get the output as 2,3,4,5


let data = [1;2;3;4]
let functiontomap  r = r+ 1
let r1 =  List.map functiontomap data
printfn "updated data  = %A" r1
open System
printfn "Press any key to continue"
Console.ReadKey(true);

 

Expected output

clip_image002

In previous example I explicitly defined the input function. However it is not required, you can use List.map as below,

image

In above way of using List.map, we are directly applying function to the input collection.


let data = [1..10]
let resultincrement = data |> List.map (fun x -> x + 1)
let resultsquare = data |> List.map (fun x -> x*x)
printfn "incremented data  = %A" resultincrement
printfn "squared data = %A" resultsquare
open System
printfn "Press any key to continue"
Console.ReadKey(true);

 

Expected output

clip_image002[6]

 

If you want to covert above integer collection as string , that also very much possible in single line of statement as below,

clip_image004

As input function you need to pass string


let data = [1..10]
let stringdata = data |> List.map string
printfn "String data = %A" stringdata
open System
printfn "Press any key to continue"
Console.ReadKey(true);

Expected output

clip_image002[8]

I hope this post was useful. Thanks for reading  Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

FSharp for beginners : Finding Square of Range of Numbers in F#

This post is targeting audience starting with F Sharp. Level: 100

We can fine square of range of numbers in F# using List.Map

If you have range of numbers from 1 to 10 declared as below,

clip_image001

And a function to calculate square as below,

clip_image002

List.Map takes two parameters. It takes a mapping and the list.

clip_image004

If you print squreNumbers you should get as output square of numbers from 1 to 10.

clip_image006

Full code is as below,

</p>
<p>let number = [1..10]<br />
let squre x= x*x<br />
let squreNumbers = List.map squre number<br />
printfn &quot;SqureNubers = %A&quot; squreNumbers<br />
open System<br />
printfn &quot;Press any key to continue&quot;<br />
Console.ReadKey(true);</p>
<p>

Press F5 to get the output,

clip_image002

I hope this post was useful. Thanks for reading  Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.