Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FSharpList<;字符串>;到IList<;字符串>;到XML_C#_F#_C# To F# - Fatal编程技术网

C# FSharpList<;字符串>;到IList<;字符串>;到XML

C# FSharpList<;字符串>;到IList<;字符串>;到XML,c#,f#,c#-to-f#,C#,F#,C# To F#,使用代码将IList转换为FSharpList,然后将列表值写入XML 公共静态类互操作 { 公共静态FSharpList-ToFSharpList(此IList输入) { 返回CreateFSharpList(输入,0); } 私有静态FSharpList CreateFSharpList(IList输入,int索引) { 如果(索引>=input.Count) { 返回FSharpList.Empty; } 其他的 { 返回FSharpList.Cons(输入[index],创建FSharp

使用代码将
IList
转换为
FSharpList
,然后将列表值写入XML

公共静态类互操作
{
公共静态FSharpList-ToFSharpList(此IList输入)
{
返回CreateFSharpList(输入,0);
}
私有静态FSharpList CreateFSharpList(IList输入,int索引)
{
如果(索引>=input.Count)
{
返回FSharpList.Empty;
}
其他的
{
返回FSharpList.Cons(输入[index],创建FSharpList(输入,索引+1));
}
}
}
我使用上面的代码来制作我自己的列表

var fsharp_distinct=distinctWords.ToFSharpList();
var distinct_不带_停止字=模块2.停止字(fsharp_distinct);
foreach(不带停止字的不同\u中的字符串wd)
添加(新元素(wd));
事实上,XML也是编写的,但就在退出循环之前,它给出了一个
System.NullReferenceException
。但是,当一个F#函数使用相同的代码返回一个
Tuple
时,我将Tuple值写入XML没有问题

编辑:我在上述问题中不正确。空点异常实际上来自以下代码:

foreach(列表2中的元组对)
colwordfreq.Root.Element(pair.Item1).Add(new-XElement(“freq”,pair.Item2));
但是当我添加条件时

if(colwordfreq.Root.Element(pair.Item1)!=null)

它没有给出例外。

您的示例有点不完整,但是如果
colwordfreq.Root.Element(pair.Item1)
为某些单词返回
null
,可能意味着您以前没有添加该元素。也许您需要通过编写以下内容来添加元素:

foreach(列表2中的元组对)
colwordfreq.Root.Add//Add元素到根目录
(新的XElement(pair.Item1,//与单词的名称)
新元素(“freq”,pair.Item2));/。。。伯爵呢
除此之外,当您从C#调用F#代码时,值得查看。其中一个建议(我强烈建议遵循)是避免使用C#中的F#特定类型(如
FSharpList
)。这些都是为F而设计的,很难从C正确使用

您可以添加一个简单的F#包装函数,该函数使用
IEnumerable
公开功能,这将更易于从C使用:

让rec停止字a=
匹配
|“the”::t |“this”::t->stopword t
|h::t->h::(停止字t)
|[] -> [] 
模块导出=
让停止字(inp:seq)=
(inp |>List.ofSeq |>stopword):>seq

然后,您可以直接从C#调用
Export.StopWord(en)
,而不显式地处理F#列表。

我认为另一个部分有例外。我在调试时发现了这一点。我编辑了这个问题。空引用来自返回单词频率的元组。为什么不先将
pair.Item1
添加到根节点,然后再将
pair.Item2
添加到
pair.Item1
?这样,您就安全了,不必创建第一个字符串列表。@pad我以前已经在XML中添加了节点。现在我尝试搜索节点并添加子节点。这里我没有添加新节点,因此,我设置了null条件。仍在测试应用程序,我以后可能需要更改它。您可以使用
SeqModule.ToList
将IList转换为F#list。感谢F#wrapper和comp design链接。我在寻找类似的信息。事实上,在XML中,我只是试图向现有节点添加子节点。如果没有现有节点,我不会添加它。这就是为什么我把空条件。