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.

2 thoughts on “FSharp for beginners : Finding Square of Range of Numbers in F#

  1. Pingback: Dew Drop – September 26, 2011 | Alvin Ashcraft's Morning Dew

  2. Pingback: Monthly Report September 2011: Total Posts 28 « debug mode……

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