FSharpList<string> to IList<string> to XML
문제
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)
returns null
for some words, it probably means that you did not add that element previously. Maybe you need to add elements by writing something like:
foreach (Tuple<string, int> pair in list2)
colwordfreq.Root.Add // Add element to the root
(new XElement(pair.Item1, // with the name of the word
new XElement("freq", pair.Item2))); // ... and the count
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#:
let rec stopword a =
match a with
|"the"::t |"this"::t -> stopword t
|h::t ->h::(stopword t)
|[] -> []
module Export =
let StopWord (inp:seq<_>) =
(inp |> List.ofSeq |> stopword) :> seq<_>
Then you can just call Export.StopWord(en)
from C# without explicitly dealing with F# lists.