[英]How to use F# to process lists from C#
我正在试图弄清楚如何使用C#程序集中的F#库,我已经使用了很多C#,但从未使用过F#。
这是F#Class ..
namespace FLib
type Class1() =
member this.square(x)=x*x
member this.doit(x, op) = List.map op (Seq.toList(x))|>List.toSeq
member this.squareAllDirect(x) = List.map this.square (Seq.toList(x))|>List.toSeq
member this.squareAllIndirect(x) = this.doit x, this.square
这是使用它的C#
class Program
{
static void Main(string[] args)
{
FLib.Class1 f = new FLib.Class1();
List<int> l=new List<int>(){1,2,3,4,5};
var q =f.squareAllDirect(l);
var r = f.squareIndirect(l);
foreach (int i in r)
Console.Write("{0},",i);
Console.ReadKey();
}
}
squareAllDirect函数按预期工作...但来自c#的squareAllIndirect调用有一个异常:方法'FLib.Class1.squareAllIndirect(System.Tuple,Microsoft.FSharp.Core.FSharpFunc'2>)'的Type参数无法推断从用法。 尝试显式指定类型参数。
它看起来你期望你的squareAllIndirect
函数采取并返回一个int seq
但是,如果你将鼠标悬停在它上面,你会看到它需要并返回一个int seq * (int -> int)
元组的优先级低于函数调用,因此x
作为两个参数传递给doit
。
您需要在()
包围函数调用的参数。
member this.squareAllIndirect(x) = this.doit(x, this.square)
这将确保您获得并返回您的期望。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.