Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building F# library to be used in C#

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.

like image 974
prosseek Avatar asked Oct 10 '10 01:10

prosseek


1 Answers

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

like image 163
Brian Avatar answered Oct 22 '22 08:10

Brian