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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:40:45+00:00 2026-05-24T16:40:45+00:00

I need help to understand how a function is working;: it is a recursive

  • 0

I need help to understand how a function is working;: it is a recursive function with yield return but I can’t figure out how it works. It is used calculate a cumulative density function (approximate) over a set of data.
Thanks a lot to everyone.

/// Approximates the cumulative density through a recursive procedure 
/// estimating counts of regions at different resolutions.
/// </summary>
/// <param name="data">Source collection of integer values</param>
/// <param name="maximum">The largest integer in the resulting cdf (it has to be a power of 2...</param>
/// <returns>A list of counts, where entry i is the number of records less than i</returns>


public static IEnumerable<int> FUNCT(IEnumerable<int> data, int max)
  {
    if (max == 1)
        {
            yield return data.Count();
        }
        else
        {
            var t = data.Where(x => x < max / 2);
            var f = data.Where(x => x > max / 2);

            foreach (var value in FUNCT(t, max / 2))
                yield return value;  

            var count = t.Count();
            f = f.Select(x => x - max / 2);
            foreach (var value in FUNCT(f, max / 2))   
                yield return value + count;
        }
    }
  • 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-24T16:40:46+00:00Added an answer on May 24, 2026 at 4:40 pm

    In essence, IEnumerable functions that use yield return function slightly differently from traditional recursive functions. As a base case, suppose you have:

    IEnumerable<int> F(int n)
    {
        if (n == 1)
        {
           yield return 1;
           yield return 2;
           // implied yield return break;
        }
        // Enter loop 1
        foreach (var v in F(n - 1))
            yield return v;
        // End loop 1
        int sum = 5;
        // Enter loop 2
        foreach (var v in F(n - 1))
            yield return v + sum;
        // End loop 2
        // implied yield return break;
    }
    void Main()
    {
       foreach (var v in F(2))
           Console.Write(v);
       // implied return
    }
    

    F takes the basic orm of the original FUNCT. If we call F(2), then walking through the yields:

    F(2)
    |   F(1)
    |   |   yield return 1
    |   yield return 1
    Console.Write(1);
    |   |  yield return 2    
    |   yield return 2
    Console.Write(2)
    |   |  RETURNS
    |   sum = 5;
    |   F(1)
    |   |  yield return 1
    |   yield return 1 + 5
    Console.Write(6)
    |   |  yield return 2
    |   yield return 2 + 5
    Console.Write(7)
    |   |  RETURNS
    |   RETURNS
    RETURNS
    

    And 1267 is printed. Note that the yield return statement yields control to the caller, but that the next iteration causes the function to continue where it had previously yielded.

    The CDF method does adds some additional complexity, but not much. The recursion splits the collection into two pieces, and computes the CDF of each piece, until max=1. Then the function counts the number of elements and yields it, with each yield propogating recursively to the enclosing loop.

    To walk through FUNCT, suppose you run with data=[0,1,0,1,2,3,2,1] and max=4. Then running through the method, using the same Main function above as a driver, yields:

    FUNCT([0,1,0,1,2,3,2,1], 4)
    | max/2 = 2
    | t = [0,1,0,1,1]
    | f = [3] // (note: per my comment to the original question,
    |         // should be [2,3,2] to get true CDF.  The 2s are
    |         // ignored since the method uses > max/2 rather than
    |         // >= max/2.)
    | FUNCT(t,max/2) = FUNCT([0,1,0,1,1], 2)
    | |    max/2 = 1
    | |    t = [0,0]
    | |    f = [] // or [1,1,1]
    | |    FUNCT(t, max/2) = FUNCT([0,0], 1)
    | |    |   max = 1
    | |    |   yield return data.count = [0,0].count = 2
    | |    yield return 2
    | yield return 2
    Console.Write(2)
    | |    |   RETURNS
    | |    count = t.count = 2
    | |    F(f, max/2) = FUNCT([], 1)
    | |    |   max = 1
    | |    |   yield return data.count = [].count = 0
    | |    yield return 0 + count = 2
    | yield return 2
    Console.Write(2)
    | |    |   RETURNS
    | |    RETURNS
    | count = t.Count() = 5
    | f = f - max/2 = f - 2 = [1]
    | FUNCT(f, max/2) = FUNCT([1], 2)
    | |    max = 2
    | |    max/2 = 1
    | |    t = []
    | |    f = [] // or [1]
    | |    FUNCT(t, max/2) = funct([], 1)
    | |    |   max = 1
    | |    |   yield return data.count = [].count = 0
    | |    yield return 0
    | yield return 0 + count = 5
    Console.Write(5)
    | |    |   RETURNS
    | |    count = t.count = [].count = 0
    | |    f = f - max/2 = []
    | |    F(f, max/2) = funct([], 1)
    | |    |   max = 1
    | |    |   yield return data.count = [].count = 0
    | |    yield return 0 + count = 0 + 0 = 0
    | yield return 0 + count = 0 + 5 = 5
    Console.Write(5)
    | |    RETURNS
    | RETURNS
    RETURNS
    

    So this returns the values (2,2,5,5). (using >= would yield the values (2,5,7,8) — note that these are the exact values of a scaled CDF for non-negative integral data, rather than an approximation).

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

Sidebar

Related Questions

I read the SDK document, but can not understand some details, need some help.
Just started working on a c project. Need help with passing function pointers/macro functions/etc.
i need help with disk_total_space function.. i have this on my code <?php $sql=select
Once again I need some help. I'm working with a table that contains 3
I've looked for some help on the reddit site itself, but I still can't
I have a very basic question and need help. I am trying to understand
Hey Guys. I need help understanding my hw assignment. I am starting out in
need help to create regular expression matching string www.*.abc.*/somestring Here * is wild card
Need help writing a script downloads data from google insight using c# this is
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat

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.