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

The Archive Base Latest Questions

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

My issue is with a certain style of code that very much resembles recursion,

  • 0

My issue is with a certain style of code that very much resembles recursion, but isn’t quite that. Recursion is, to quote Wikipedia, “a method of defining functions in which the function being defined is applied within its own definition”. Similarly mutual recursion applies another function which, directly or indirectly, applies the function we are defining.

The problem is that the code I am thinking of, and dealing with, does not use the same function! It uses the same code in another function (as a method or closure).

The problem here is that while my code is the same, the functions are not. Take a look a the following basic mutual recursion example:

def is_even(x):
    if x == 0:
        return True
    else:
        return is_odd(x - 1)

def is_odd(x):
    if x == 0:
        return False
    else:
        return is_even(x - 1)

This is somewhat intuitive, and very clearly mutually recursive. However, if I wrap each function up as an inner function that is created once every call, it gets less clear:

def make_is_even(): 
    def is_even(x):
        if x == 0:
            return True
        else:
           return make_is_odd()(x - 1)
    return is_even

def make_is_odd(): 
    def is_odd(x):
        if x == 0:
            return False
        else:
            return make_is_even()(x - 1)
    return is_odd

def is_even2(x):
    return make_is_even()(x)

def is_odd2(x):
    return make_is_odd()(x)

Disregarding optimizations like implicit memoization etc., this produces a chain of function calls that isn’t strictly recursive, creating and calling various new functions without ever calling the same one twice. Nonetheless all these functions follow a common template, and are just the same function created over and over again (possibly with different free variables.

And again, we can come up with a directly equivalent (after all, classes are really just closures, right 😉 implementation using classes. This is especially significant, since this style of [insert name here] is used in, e.g., the Composite Pattern. The difference is that with the Composite design pattern, and most uses (even of the closures), the instances are not usually created on the fly. It is still essentially the same.

class EvenChecker(object):
    def check(self, x):
        if x == 0:
            return True
        else:
            return OddChecker().check(x - 1)

class OddChecker(object):
    def check(self, x):
        if x == 0:
            return False
        else:
            return EvenChecker().check(x - 1)

def is_even3(x):
    return EvenChecker().check(x)

def is_odd3(x):
    return OddChecker().check(x)

This time the chain is of object creation and method calls, but the principle is the same. (I would actually note that it’s slightly different, in that Python defines a simple wrapper on a per-object basis which itself calls the very same function every time– but this is not necessarily something we need to know, and it doesn’t need to be true for other implementations of classes and objects. But yes, strictly speaking it is mutually recursive, as well as… something more, and it’s that other thing that I want to know.)

  • 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-14T07:49:15+00:00Added an answer on May 14, 2026 at 7:49 am

    As you point out, this is still mutual recursion. I don’t think the “something more” you’re asking about has a name; if it does I’ve never heard it.

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

Sidebar

Related Questions

The issue that prompted me to ask this is a web form that was
Our issue is that our project has files being downloaded using wget to the
Here is the issue I am having: I have a large query that needs
I am having a couple of issues deciding how best to describe a certain
This issue came up when I got different records counts for what I thought
The issue is there is a database with around 20k customer records and I
another issue in IE 6... link The navbar link list has a border applied
This issue is driving me mad. I have several tables defined, and CRUD stored
The issue is simple really. Instead of creating folders in Visual Studio, I create
The issue I have is as follows: My company's supplier gives us an Access

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.