Links: Up
This sample shows how to do simple cross-language programming involving F# and C#. In particular it shows how to access the functionality of F# modules from C#, including how to access F#'s discriminated unions, tuples, records and function values. On Windows build the sample using build.bat and on Unix build using build.sh.
The first file (FSharpLibrary.fs) defines a software component using F#. The component publishes some simple types and functions, specified in the interface file (FSharpLibrary.fsi). The (CSharpProgram.cs) shows how the F# component can be used from C#.
let Factorize(factorizableNum) = let primefactor1 = ref 1 in let primefactor2 = ref factorizableNum in let i = ref 2 in let fin = ref false in while (!i < factorizableNum && not !fin) do if (factorizableNum mod !i = 0) then begin primefactor1 := !i; primefactor2 := factorizableNum / !i; fin := true; end; i := !i + 1; done; if (!primefactor1 = 1) then None else Some (!primefactor1, !primefactor2)