Monday, November 3, 2014

Loop construct of C#/VB mapped into F#

Hi there, my blog readers!
The F# adventure series is almost complete, and F# adventure series is focusing on F# 3.0 in Visual Studio 2012. Meanwhile, there were questions addressed to me about the basic common feature of programming languages: loop.
For those of you comes from C#/VB world, F# actually has the same semantic loop constructs.
F# has these loop supports:
  1. for loops with n times (incremental for in C#,For Next in VB)
  2. for loops that iterate the elements of a collection/set/array (for each in C#, For Each in VB since VB 8.0 in Visual Studio 2005)
  3. do..while that perform loop with test condition (same as do..while in C#, While..Wend in VB)
To simplify giving you the samples, I’ll take samples of F# directly from MSDN Library, paste them, and add the same samples with C# and VB.
Starts from Visual F# MSDN Library: http://msdn.microsoft.com/en-us/library/dd233249.aspx
Let’s visit the three loop above with samples:



F# loop C# VB
// for loop with n times

for i = 1 to 10 do
  printf "%d " i

// incremental for

for (var i=1;i++;i<=10)
   Console.Write("{0:d}",i);
‘ For Next

For I = 1 to 10
    Console.Write("{0:d}",i)
Next

// Looping over a list.
let list1 = [ 1; 5; 100; 450; 788 ]
for i in list1 do
   printfn "%d" i
// for each
var list1 = new Int32 { 1, 5, 100, 450, 788 }
for each i in list1
   Console.WriteLine("{0:d}",i);
‘ For Each
Dim list1 As Int32() = [ 1, 5, 100, 450, 788 ]
For Each i In list1
   Console.WriteLine("{0:d}",i)
End For
// while..do


let lookForValue value maxValue =
  let mutable continueLooping = true
  let randomNumberGenerator = new Random()
  while continueLooping do
    // Generate a random number between 1 and maxValue.
    let rand = randomNumberGenerator.Next(maxValue)
    printf "%d " rand
    if rand = value then
       printfn "\nFound a %d!" value
       continueLooping <- false


// do

void lookForValue(Int32 value, Int32 maxValue)
{
    var continueLooping= true;
    var randomNumberGenerator = Rendom();
    do
    {
        var rand = randomNumberGenerator.Next(maxValue)
        Console.Write(rand);
        if (rand == value)
        {
             Console.WriteLine(rand);
             continueLooping = false;
        }
    } while (continueLooping)
}
‘ While..Wend

Sub lookForValue(value As Int32, maxValue As Int32)
    Dim continueLooping= True
    Dim randomNumberGenerator = Rendom()
    While continueLooping
        Dim rand = randomNumberGenerator.Next(maxValue)
        Console.Write(rand)
        If (rand == value)
             Console.WriteLine(rand)
             continueLooping = false
        End If
    End While
End Sub

There you have it now! All of those samples mapped from F# codes are semantically the same, although the low level IL implementation might be different.

In For Next, VB will hoist the variable of the loop limit first. But this will change after Roslyn compiler is shipped with Visual Studio 2015, as C# and VB now have the same compiler infrastructure.

Great question my blog reader, and I will include this in my completed F# adventure series!

No comments:

Post a Comment