Sunday, February 1, 2009

F# - Hello World

A simple Hello World code in F#:

#light
let HelloWorld =
    printfn "Hello World"

HelloWorld

It is the IL code for the F# code above:

Code to execute in IL:

  1: .class private abstract auto ansi sealed beforefieldinit $Program
  2:     extends [mscorlib]System.Object
  3: {
  4:     .method public static void _main() cil managed
  5:     {
  6:         .entrypoint
  7:         .maxstack 3
  8:         L_0000: nop 
  9:         L_0001: ldstr "Hello World"
 10:         L_0006: newobj instance void [FSharp.Core]Microsoft.FSharp.Text.Format`5<class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [mscorlib]System.IO.TextWriter, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit>::.ctor(string)
 11:         L_000b: call !!0 [FSharp.Core]Microsoft.FSharp.Core.Pervasives::printfn<class [FSharp.Core]Microsoft.FSharp.Core.Unit>(class [FSharp.Core]Microsoft.FSharp.Text.Format`4<!!0, class [mscorlib]System.IO.TextWriter, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit>)
 12:         L_0010: stsfld class [FSharp.Core]Microsoft.FSharp.Core.Unit <StartupCode$ConsoleApplication1>.$Program::HelloWorld@19
 13:         L_0015: call class [FSharp.Core]Microsoft.FSharp.Core.Unit Program::get_HelloWorld()
 14:         L_001a: pop 
 15:         L_001b: ret 
 16:     }
 17: 
 18: 
 19:     .field assembly static class [FSharp.Core]Microsoft.FSharp.Core.Unit HelloWorld@19
 20: 
 21: }
 22: 
 23: 

The same code in C#:

  1: internal static class $Program
  2: {
  3:     // Fields
  4:     internal static Unit HelloWorld@19;
  5: 
  6:     // Methods
  7:     public static void _main()
  8:     {
  9:         HelloWorld@19 = Pervasives.printfn<Unit>(new Format<Unit, TextWriter, Unit, Unit, Unit>("Hello World"));
 10:         Program.get_HelloWorld();
 11:     }
 12: }
 13: 
 14: 

Something similar in C#:

  1: class Program
  2: {
  3:     static void Main(string[] args)
  4:     {
  5:         Console.WriteLine("Hello World");
  6:     }
  7: }

It is the IL code for the C# code above:

  1: .class private auto ansi beforefieldinit Program
  2:     extends [mscorlib]System.Object
  3: {
  4:     .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
  5:     {
  6:         .maxstack 8
  7:         L_0000: ldarg.0 
  8:         L_0001: call instance void [mscorlib]System.Object::.ctor()
  9:         L_0006: ret 
 10:     }
 11: 
 12:     .method private hidebysig static void Main(string[] args) cil managed
 13:     {
 14:         .entrypoint
 15:         .maxstack 8
 16:         L_0000: nop 
 17:         L_0001: ldstr "Hello World"
 18:         L_0006: call void [mscorlib]System.Console::WriteLine(string)
 19:         L_000b: nop 
 20:         L_000c: ret 
 21:     }
 22: 
 23: }
 24: 
 25: 

No comments:

Post a Comment