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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:27:03+00:00 2026-05-29T13:27:03+00:00

A recursive list is represented by a chain of pairs. The first element of

  • 0

A recursive list is represented by a chain of pairs. The first element of each pair is an element in the list, while the second is a pair that represents the rest of the list. The second element of the final pair is None, which indicates that the list has ended. We can construct this structure using a nested tuple literal. Example:

(1, (2, (3, (4, None))))

So far, I’ve created a method that converts a tuple of values or the value None into a corresponding rlist. The method is called to_rlist(items). Example:

>>> to_rlist((1, (0, 2), (), 3))
(1, ((0, (2, None)), (None, (3, None))))

How do I write the inverse of to_rlist, a function that takes an rlist as input and returns the corresponding tuple? The method should be called to_tuple(parameter). Example of what should happen:

>>> x = to_rlist((1, (0, 2), (), 3)) 
>>> to_tuple(x)
(1, (0, 2), (), 3)

Note: The method to_rlist works as intended.

This is what I have so far:

def to_tuple(L):
    if not could_be_rlist(L):         
        return (L,)
    x, y = L
    if not x is None and not type(x) is tuple and y is None:         
        return (x,)     
    elif x is None and not y is None:         
        return ((),) + to_tuple(y)
    elif not x is None and not y is None:         
        return to_tuple(x) + to_tuple(y)

Which gives me the following result (which is incorrect):

>>> x = to_rlist((1, (0, 2), (), 3)) 
>>> to_tuple(x)
(1, 0, 2, (), 3)

How can I fix my method to return a nested tuple properly?

  • 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-29T13:27:04+00:00Added an answer on May 29, 2026 at 1:27 pm
    def to_list(x):
        if x == None:
            return ()
        if type(x) != tuple:
            return x
        a, b = x
        return (to_list(a),) + to_list(b)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple recursive function that look over list and return
I'm trying to write a recursive function that return a List < int *
I recently started with F# and implemented a very basic recursive function that represents
I have a class that does some recursive merge sorting on a generic List,
I’m trying to implement a tail-recursive list-sorting function in OCaml, and I’ve come up
Is it possible to define a recursive list comprehension in Python? Possibly a simplistic
I have a recursive function working on a list, the function contains a loop
I have a recursive object, a linked list really: public class LinkedList { public
any tail-recursive version for the below mentioned pseudocode ? Thanks ! (define (min list)
So I'm using recursive blocks. I understand that for a block to be recursive

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.