views:

150

answers:

1

Let's say I have the following function.

let rec fib n =
    match n with
    | 1 | 2 -> 1
    | n -> fib(n-1) + fib(n-2)

How can I compile this code into dll so that it's used from C#?

I use both mono (in Mac OS X) and Visual Studio 2010.

ADDED

I added the following to make namespace.

namespace MyMath.Core
module public Process=

And fsc --target:library facto.fs gives me facto.dll.

The C# code to use this code as follows.

using System;
using MyMath.Core;

class TestFSharp {
    public static void Main() {
        int val = MyMath.Core.Process.fib(10);
        Console.WriteLine(val);
    }
}

dmcs /r:facto.dll testf.cs gives me tesetf.exe.

+8  A: 

Well, if you put this in the file Program.fs, and compile it into a library (with --target:library), then it would appear from C# as

Program.fib(5)

See also http://stackoverflow.com/questions/736956/call-f-dll-in-c

However if you plan to expose F# code to C#, you may want to uses namespaces and types. See the F# Component Design Guidelines for more.

Brian