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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:00:56+00:00 2026-05-14T17:00:56+00:00

Consider a function f(x,y): f(x,0) = x*x; f(0,y) = y*(y + 1); f(x,y) =

  • 0

Consider a function f(x,y):

f(x,0) = x*x;
f(0,y) = y*(y + 1);
f(x,y) = f(x,y-1) + f(x-1,y);

If one tries to implement that recursively in some language like C++ he will encounter a problem.

Suppose the function is first called with x = x0 and y = y0. Then for any pair (x,y) where 0 <= x < x0 and 0 <= y < y0 the intermediate values will be computed multiple times – recursive calls will form a huge tree in which multiple leaves will in fact contain the same pairs (x,y). For pairs (x,y) where x and y are both close to 0 values will be computed numerous times.

For instance, I tested a similar function implemented in C++ – for x=20 and y=20 its computation takes about 4 hours (yes, four Earth hours!).

Obviously the implementation can be rewritten in such way that repeated computation doesn’t occur – either iteratively or with a cache table.

The question is: will functional languages perform any better and avoid repeated computations when implementing a function like above recursively?

  • 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-14T17:00:56+00:00Added an answer on May 14, 2026 at 5:00 pm

    The term you’re looking for here is Memoization:

    In computing, memoization is an
    optimization technique used primarily
    to speed up computer programs by
    having function calls avoid repeating
    the calculation of results for
    previously-processed inputs.

    No, functional language do not automatically implement memoization. You can implement it in them, but in C++ as well. It is true, however, that when you write purely functional code (i.e. with no side effects), then memoization is easier. Some dynamic languages (Perl, for instance) have auto-memoization modules that can easily memoize any function. There’s a discussion of this subject in the Automatic memoization section of the Wikipedia article.

    For example here’s a naive C++ Fibonacci:

    long fib_rec(long index)
    {
        if (index < 2)
            return index;
        else
            return fib_rec(index - 1) + fib_rec(index - 2);
    }
    

    And here’s a memoized version:

    long fib_memoized_aux(vector<long>& cache, long index)
    {
        if (cache[index] >= 0)
        return cache[index];
    
        cache[index] = fib_memoized_aux(cache, index - 1) + fib_memoized_aux(cache, index - 2);
        return cache[index];
    }
    
    
    long fib_memoized(long index)
    {
        vector<long> cache(index + 1, -1);
        cache[0] = 0;
        cache[1] = 1;
    
        return fib_memoized_aux(cache, index);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider some function foo : def foo(input) input * 2 end How to get
Consider the following function: void f(const char* str); Suppose I want to generate a
Consider the hypothetical function repeatcall , that takes as arguments a no-args callable func
Consider we have a multi-level html list that looks like this: <ul class=catalog> <li>
Consider a class that is supposed to make parameter suggestion, given some clues, and
Consider the following Javascript function (1): function setData(domElement) { domElement.myDataProperty = { 'suppose': 'this',
This appeared as some test question. If you consider this function which uses a
Consider the following definition: f[x_]=Piecewise[{{0,x<1/2},{Interval[{0,1}],x==1/2},{1,x>1/2}}]; Then when one does the Plot[f[x],{x,0,1}] of the function,
I've two different template classes. One of them has a member function that returns
Consider: function Shape() { this.name = "Generic"; this.draw = function() { return "Drawing "

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.