Using a code to convert IList<T> to an FSharpList<T> then write the list values to a XML.
public static class Interop
{
public static FSharpList<T> ToFSharpList<T>(this IList<T> input)
{
return CreateFSharpList(input, 0);
}
private static FSharpList<T> CreateFSharpList<T>(IList<T> input, int index)
{
if(index >= input.Count)
{
return FSharpList<T>.Empty;
}
else
{
return FSharpList<T>.Cons(input[index], CreateFSharpList(input, index + 1));
}
}
}
I use the above code to make my own list
var fsharp_distinct = distinctWords.ToFSharpList();
var distinct_without_stopwords = Module2.stopword(fsharp_distinct);
foreach (string wd in distinct_without_stopwords)
colwordfreq.Root.Add(new XElement(wd));
Infact, the XML is written too but just before exiting the loop it gives a System.NullReferenceException. But when a F# function returned a Tuple<string, int> using the same code I had no problems writing the Tuple values to a XML.
EDIT: I was not correct in the above question. The null point exception actually came from this code:
foreach (Tuple<string, int> pair in list2)
colwordfreq.Root.Element(pair.Item1).Add(new XElement("freq", pair.Item2));
But when I added the condition
if (colwordfreq.Root.Element(pair.Item1) != null)
It doesn’t give that exception.
Your example is a bit incomplete, but if
colwordfreq.Root.Element(pair.Item1)returnsnullfor some words, it probably means that you did not add that element previously. Maybe you need to add elements by writing something like:Aside, when you’re calling F# code from C#, it is worth checking out the F# component design guidelines. One of the suggestions there (which I strongly recommend to follow) is to avoid using F# specific types (like
FSharpList) from C#. These are really designed for F# and are hard to use correctly from C#.You can add a simple F# wrapper function that exposes the functionality using
IEnumerable, which will be a lot easier to use from C#:Then you can just call
Export.StopWord(en)from C# without explicitly dealing with F# lists.