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

5 thoughts on “F# vs C# Samples

    1. Hi,
      Probably I am missing something about your comment due to a typo error.
      List.iter looks for a function to evaluate and “fun” is the keyword to start to define a function.
      And what we want to achieve is to print after the function evaluates the value, i.e. if bigger than 5 print.
      With your example, the line would be like this?
      List.iter (printfn “%d” (fun x -> x> 5))

  1. Try:

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

    and

    let dir = Directory.GetFiles >> Array.iter (printfn “%s”)
    dir “c:\\”

    I find it a bit more succinct and readable. For the first one, I’d have used Seq instead of List, since that’s more generalisable and you’re only going forward in any case.

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