Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7549691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:00:40+00:00 2026-05-30T10:00:40+00:00

Using a code to convert IList<T> to an FSharpList<T> then write the list values

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:00:42+00:00Added an answer on May 30, 2026 at 10:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this code to convert a Set to a List : Map<String,
i am using following code to convert xml file to datatable but its just
I want to convert IList to array: Please see my code: IList list =
How can I convert a character to its ASCII code using JavaScript? For example:
I'm using php and I have the following code to convert an absolute path
Is there a way/tool to auto convert Java source code from using raw types
We try to convert from string to Byte[] using the following Java code: String
I am using following code to convert string to sha1 string but i am
I am using the following code to convert js object to json but not
i am using the following code to convert an excel set of data on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.