Partager via


Adventures in F#--The Lay of the Land

Jomo Fisher--I'm taking some time now to better understand F#. Right now, I understand the concept of functional code and have some narrow but deep experience by way of the work we did on LINQ for C# 3.0. My goal is to understand F# as well as I understand C#.

First, I downloaded and installed F# from here. Nice that there is source code for the compiler in there. I'll take a closer look at that down the road.

For now, a first program. I put this into a file test.fs:

let rec factorial n = if n <= 1 then 1 else n * factorial (n-1)

That 'rec' next to the 'let' means recursive. So, unlike C# and VB, F# requires an explicit claim of recursiveness. This would let me grep for all recursive functions in a project. Also there's no class definition required. This is actually pretty nice because it gives a very simple starting point. I don't see a reason that C# couldn't work this way as well.

Compile it:

fsc test.fs

And get a warning:

test.fs(1,0): warning: Main module of program is empty: nothing will happen when it is run.

Ok, so this gave me an .exe called test.exe which I expect to have a factorial function in it somewhere. When I put test.exe under Reflector (thanks, as always, Lutz Roeder) and disassemble into C# I see this:

 [CompilationMapping(SourceLevelConstruct.Module)]
public class Test
{
    // Methods
    public static int factorial(int n)
    {
        if ((n > 1) ^ true)
        {
            return 1;
        }
        return (n * factorial(n - 1));
    }
}

First, that CompilationMapping attribute is coming from Microsoft.FSharp.Core. I assume this attribute means 'the global methods for this module'. The Test class is in the global namespace. That (n>1)^true is a roundabout way of saying n<=1. Not the most readable thing but I'm not faulting F# for not being disassembleable into pretty C#. Also, I'm not worried about performance. For all I know, the jitter reduces this just fine. If it doesn't, I'm still not worried because performance typically comes from good algorithms, not micro-optimizations.

 

This posting is provided "AS IS" with no warranties, and confers no rights.

Comments

  • Anonymous
    September 12, 2007
    Jomo Fisher--I was curious about type inference in F# and I wondered what would happen if there was really

  • Anonymous
    September 12, 2007
    Jomo Fisher--I was curious about type inference in F# and I wondered what would happen if there was really

  • Anonymous
    September 12, 2007
    Interesting that the "rec" declaration for this method is not captured in the metadata.  I might expect to see an attribute representing this to make it inspectable at runtime.

  • Anonymous
    September 13, 2007
    Acutally, the rec keyword would be a lovely definition to C# lambdas. Making them recursive isn't the prettiest thing in the world.

  • Anonymous
    September 17, 2007
    Jomo Fisher-- Easily my favorite feature of F# so far is the combination of discriminated union and pattern