A couple of questions left

Now, I have some time to write about a couple of questions I have received about F#, and their answers.

1. To get a specific element of a tuple :

If we do not want to create variables for the elements that we are not interested in of a tuple, we can use “_” for all other elements, i.e.:

let drinks= (Coke, 0.45, “Coca Cola Zero”)
let _, _, valBrand= drinks

which is interpreted into the compiler as we see in the FS Interactive window:

val drinks : string * float * string = (“Coke”, 0.45, “Coca Cola Zero”)
val valBrand : string = “Coca Cola Zero”

2. “namespace” keywords stays as is, and we can have either a module or a type [i.e. C# class] under a namespace. The example would be :
File1: Drink.fs

namespace Vesta.Beverage
module Drink=
let drinks= (Coke, 0.45, “Coca Cola Zero”)
let _, _, valBrand= drinks

File2: BeverageCalculator.fs [placed under the Drink.fs if they are in the same project.]

namespace Vesta.Calculator
open Vesta.Beverage.Drink
module BeverageCalculator=
let drink= valBrand

So, we can use everything from the first namespace,
Happy F# days!

F# vs C# Samples

After my talk at ldnUG yesterday, I got nice suggestions from Zi Makki, and here is the first one: comparison between F# and C#, especially using LINQ. This subject had been quite popular once in a while, now it is probably my time to focus on this subject. My initial samples are quite common: getting numbers less than 5 from a list, and getting files from a specified directory.

static void Main(string [] args)
{
List list= new List { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
LessThanFive(list);
}
private static void LessThanFive(List list)
{
string y = list
.Where(x => x <= 5)
.Select(z => z.ToString())
.Aggregate(” “, (seed, n) => seed + n);
}

If we want to write this in F#, List.filter will be enough for filter operation, and the List.iter will go through the list, and do any operation/function we define for each item in the list.

let lessThanFive list=
list
|> List.filter ((>=) 5) >>
|> List.iter (printfn “%d”)
let z = lessThanFive [1..9]

The second example if for printing the folder names
With the help of the extension method explained at stackoverflow, here is a better C# code to get the files of a specified path.

class Program
{
static void Main(string[] args)
{
Dir(@”C:\Program Files (x86)\Microsoft F#\v4.0″);
Console.ReadLine();
}

private static void Dir(string
{
Directory.GetFiles(path)
.ForEachWithIndex(
(item, idx) => Console.WriteLine(“{0}: {1}”, item, idx));
}
}
public static class ForEachExtensions
{
public static void ForEachWithIndex(this IEnumerable enumerable, Action <T,int> handler)
{
int idx = 0;
foreach (T item in enumerable)
handler(item, idx++);
}
}

And the F# version would be quite simple, as Array.iter is enough to process the array resulting from GetFiles:

let dir folder=
Directory.GetFiles folder
|> Array.iter(fun x-> printfn “%s” x)
let result= dir @”C:\Program Files (x86)\Microsoft F#\v4.0″

VS2010 installation dissappointment

Yesterday I have installed my VS2010 RC, but did not work when I try to set F# as default settings, and try to open a new project [any project unfortunately]
The error message is :
“The project file ‘C:\Users\Ebru\AppData\Local\Temp\qxdqjrxq.oco\Temp\Library1.fsproj’ cannot be opened. because its project type (.fsproj) is not supported by this version of the application.
To open it, please use a version that supports this type of project.”

here is my screenshot…Image

How dissapointing is that I have to install R# Runtime separately…And to fix this you may re-run this exe and repair it.
Then, you may start your F# project.

And
Image