Updates from a continuous developer

Just another WordPress.com weblog

F# vs C# Samples

with 5 comments

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″
About these ads

Written by continuousdevelopment

May 13, 2010 at 11:00 pm

Posted in .net, F#

5 Responses

Subscribe to comments with RSS.

  1. (fun x -> printfn “%d” x)

    may be replaced with:

    (printfn “%d”)

    and (fun x -> x) 5)

    Controlflow

    May 14, 2010 at 11:40 am

    • 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))

      continuousdevelopment

      May 15, 2010 at 8:03 am

  2. [...] Ebru Cucen’s F# vs C# Samples [...]

  3. [...] This post was mentioned on Twitter by dmohl. dmohl said: "F# vs C# Samples" by @ebrucucen http://bit.ly/atveI8 #fsharp [...]

  4. 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.

    Yusuf Motara

    May 17, 2010 at 8:11 am


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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: