gpt4 book ai didi

c# - 从 C# 调用 F# 代码

转载 作者:IT王子 更新时间:2023-10-29 03:40:22 25 4
gpt4 key购买 nike

我正在玩弄 F# 和 C#,想从 C# 调用 F# 代码。

我设法让它在 Visual Studio 中以相反的方式工作,方法是在同一解决方案中有两个项目,并将 C# 代码的引用添加到 F# 项目。完成此操作后,我可以调用 C# 代码,甚至可以在调试时单步执行它。

我想做的是从 C# 编写 F# 代码,而不是从 F# 编写 C# 代码。我在 C# 项目中添加了对 F# 项目的引用,但它没有像以前那样工作。我想知道如果不手动执行是否可行。

最佳答案

下面是从 C# 调用 F# 的工作示例。

正如您所遇到的,我无法通过从“添加引用...项目”选项卡中进行选择来添加引用。相反,我不得不手动执行此操作,方法是在“添加引用 ... 浏览”选项卡中浏览到 F# 程序集。

------ F# 模块 ------

// First implement a foldl function, with the signature (a->b->a) -> a -> [b] -> a
// Now use your foldl function to implement a map function, with the signature (a->b) -> [a] -> [b]
// Finally use your map function to convert an array of strings to upper case
//
// Test cases are in TestFoldMapUCase.cs
//
// Note: F# provides standard implementations of the fold and map operations, but the
// exercise here is to build them up from primitive elements...

module FoldMapUCase.Zumbro
#light


let AlwaysTwo =
2

let rec foldl fn seed vals =
match vals with
| head :: tail -> foldl fn (fn seed head) tail
| _ -> seed


let map fn vals =
let gn lst x =
fn( x ) :: lst
List.rev (foldl gn [] vals)


let ucase vals =
map String.uppercase vals

----- 模块的 C# 单元测试 -----

// Test cases for FoldMapUCase.fs
//
// For this example, I have written my NUnit test cases in C#. This requires constructing some F#
// types in order to invoke the F# functions under test.


using System;
using Microsoft.FSharp.Core;
using Microsoft.FSharp.Collections;
using NUnit.Framework;

namespace FoldMapUCase
{
[TestFixture]
public class TestFoldMapUCase
{
public TestFoldMapUCase()
{
}

[Test]
public void CheckAlwaysTwo()
{
// simple example to show how to access F# function from C#
int n = Zumbro.AlwaysTwo;
Assert.AreEqual(2, n);
}

class Helper<T>
{
public static List<T> mkList(params T[] ar)
{
List<T> foo = List<T>.Nil;
for (int n = ar.Length - 1; n >= 0; n--)
foo = List<T>.Cons(ar[n], foo);
return foo;
}
}


[Test]
public void foldl1()
{
int seed = 64;
List<int> values = Helper<int>.mkList( 4, 2, 4 );
FastFunc<int, FastFunc<int,int>> fn =
FuncConvert.ToFastFunc( (Converter<int,int,int>) delegate( int a, int b ) { return a/b; } );

int result = Zumbro.foldl<int, int>( fn, seed, values);
Assert.AreEqual(2, result);
}

[Test]
public void foldl0()
{
string seed = "hi mom";
List<string> values = Helper<string>.mkList();
FastFunc<string, FastFunc<string, string>> fn =
FuncConvert.ToFastFunc((Converter<string, string, string>)delegate(string a, string b) { throw new Exception("should never be invoked"); });

string result = Zumbro.foldl<string, string>(fn, seed, values);
Assert.AreEqual(seed, result);
}

[Test]
public void map()
{
FastFunc<int, int> fn =
FuncConvert.ToFastFunc((Converter<int, int>)delegate(int a) { return a*a; });

List<int> vals = Helper<int>.mkList(1, 2, 3);
List<int> res = Zumbro.map<int, int>(fn, vals);

Assert.AreEqual(res.Length, 3);
Assert.AreEqual(1, res.Head);
Assert.AreEqual(4, res.Tail.Head);
Assert.AreEqual(9, res.Tail.Tail.Head);
}

[Test]
public void ucase()
{
List<string> vals = Helper<string>.mkList("arnold", "BOB", "crAIg");
List<string> exp = Helper<string>.mkList( "ARNOLD", "BOB", "CRAIG" );
List<string> res = Zumbro.ucase(vals);
Assert.AreEqual(exp.Length, res.Length);
Assert.AreEqual(exp.Head, res.Head);
Assert.AreEqual(exp.Tail.Head, res.Tail.Head);
Assert.AreEqual(exp.Tail.Tail.Head, res.Tail.Tail.Head);
}

}
}

关于c# - 从 C# 调用 F# 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/478531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com