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 262259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:30:02+00:00 2026-05-11T22:30:02+00:00

I like to define sequences recursively as follows: let rec startFrom x = seq

  • 0

I like to define sequences recursively as follows:

let rec startFrom x =
    seq {
        yield x;
        yield! startFrom (x + 1)
    }

I’m not sure if recursive sequences like this should be used in practice. The yield! appears to be tail recursive, but I’m not 100% sure since its being called from inside another IEnumerable. From my point of view, the code creates an instance of IEnumerable on each call without closing it, which would actually make this function leak memory as well.

Will this function leak memory? For that matter is it even “tail recursive”?

[Edit to add]: I’m fumbling around with NProf for an answer, but I think it would be helpful to get a technical explanation regarding the implementation of recursive sequences on SO.

  • 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-11T22:30:03+00:00Added an answer on May 11, 2026 at 10:30 pm

    I’m at work now so I’m looking at slightly newer bits than Beta1, but on my box in Release mode and then looking at the compiled code with .Net Reflector, it appears that these two

    let rec startFromA x =    
        seq {        
            yield x     
            yield! startFromA (x + 1)    
        }
    
    let startFromB x =    
        let z = ref x
        seq {        
            while true do
                yield !z
                incr z
        }
    

    generate almost identical MSIL code when compiled in ‘Release’ mode. And they run at about the same speed as this C# code:

    public class CSharpExample
    {
        public static IEnumerable<int> StartFrom(int x)
        {
            while (true)
            {
                yield return x;
                x++;
            }
        }
    }
    

    (e.g. I ran all three versions on my box and printed the millionth result, and each version took about 1.3s, +/- 1s). (I did not do any memory profiling; it’s possible I’m missing something important.)

    In short, I would not sweat too much thinking about issues like this unless you measure and see a problem.

    EDIT

    I realize I didn’t really answer the question… I think the short answer is “no, it does not leak”. (There is a special sense in which all ‘infinite’ IEnumerables (with a cached backing store) ‘leak’ (depending on how you define ‘leak’), see

    Avoiding stack overflow (with F# infinite sequences of sequences)

    for an interesting discussion of IEnumerable (aka ‘seq’) versus LazyList and how the consumer can eagerly consume LazyLists to ‘forget’ old results to prevent a certain kind of ‘leak’.)

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

Sidebar

Related Questions

I have a macro that looks like this: #define coutError if (VERBOSITY_SETTING >= VERBOSITY_ERROR)
For example, never define a macro like this: #define DANGER 60 + 2 This
I want to define something like this in php : $EL = \n<br />\n;
If I define a struct in C# using automatic properties like this: public struct
As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...)
I would like to be able to define and use a custom type in
I have to define the grammar of a file like the one shown below.
Is there a way to define the weekly period I would like to use
If I create a class like so: // B.h #ifndef _B_H_ #define _B_H_ class
I have an xml like this: <todo> <doLaundry cost=1/> <washCar cost=10/> <tidyBedroom cost=0 experiencePoints=5000/>

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.