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

  • Home
  • SEARCH
  • 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 884969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:49:55+00:00 2026-05-15T12:49:55+00:00

How can a value of type: type Tree = | Node of int *

  • 0

How can a value of type:

type Tree =
    | Node of int * Tree list

have a value that references itself generated in a functional way?

The resulting value should be equal to x in the following Python code, for a suitable definition of Tree:

x = Tree()
x.tlist = [x]

Edit: Obviously more explanation is necessary. I am trying to learn F# and functional programming, so I chose to implement the cover tree which I have programmed before in other languages. The relevant thing here is that the points of each level are a subset of those of the level below. The structure conceptually goes to level -infinity.

In imperative languages a node has a list of children which includes itself. I know that this can be done imperatively in F#. And no, it doesn’t create an infinite loop given the cover tree algorithm.

  • 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-15T12:49:56+00:00Added an answer on May 15, 2026 at 12:49 pm

    Tomas’s answer suggests two possible ways to create recursive data structures in F#. A third possibility is to take advantage of the fact that record fields support direct recursion (when used in the same assembly that the record is defined in). For instance, the following code works without any problem:

    type 'a lst = Nil | NonEmpty of 'a nelst
    and 'a nelst = { head : 'a; tail : 'a lst }
    
    let rec infList = NonEmpty { head = 1; tail = infList }
    

    Using this list type instead of the built-in one, we can make your code work:

    type Tree = Node of int * Tree lst
    let rec x = Node(1, NonEmpty { head = x; tail = Nil })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In .NET, a value type (C# struct ) can't have a constructor with no
What column type in Oracle 10g can be used to store any value of
In .Net you can read a string value into another data type using either
I know you can't rely on equality between double or decimal type values normally,
The html helper methods check the ViewDataDictionary for a value. The value can either
Can every possible value of a float variable can be represented exactly in a
How can dynamic Key-value pairs of objects be stored in app.config in using the
How can i set value(in code behind) for an element declared in the xslt
How can I calculate the value of PI using C#? I was thinking it
Can I convert a string representing a boolean value (e.g., 'true', 'false') into an

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.