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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:49:28+00:00 2026-05-17T23:49:28+00:00

Say I have the list ‘(1 2 3 (4 5 6) 7 8 9).

  • 0

Say I have the list ‘(1 2 3 (4 5 6) 7 8 9). It should return (9 8 7 (6 5 4) 3 2 1) using the following code below. I’m trying to understand how this iterative process works. Showing how this is done step by step would be very helpful.

The part I get confused the most is when deep-reverse is called twice at this point
(append
(deep-reverse (cdr lst))
(list (deep-reverse (car lst)))))

I don’t know what happens then.

(define (deep-reverse lst) 
   (cond ((null? lst) '() ) 
         ((pair? (car lst)) 
          (append 
           (deep-reverse (cdr lst)) 
           (list (deep-reverse (car lst))))) 
         (else 
          (append 
           (deep-reverse (cdr lst)) 
           (list (car lst)))))) 
  • 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-17T23:49:29+00:00Added an answer on May 17, 2026 at 11:49 pm

    Well, I just answered something like this here, but I didn’t really go into detail on the deep-reverse.

    What happens is it reverses each item that happens to be a list before appending it to the end of the list. Imagine what would happen if it didn’t call deep-reverse on the car of the list: (reverse '(a (b c d) e) is

    (list
       'e
       '(b c d)
       'a
    )
    

    With deep-reverse it would look something like

    (list
        'e
        (deep-reverse '(b c d))
        'a
    )
    

    Which is

    (list
        'e
        '(d c b)
        'a
    )
    

    Here is another version of deep-reverse, written differently, if that makes it clearer.

    (define (deep-reverse ls)
      (define (deep-reverse-2 ls acc)
        (if (null? ls)
            acc
            (if (list? (car ls))
                (deep-reverse-2 (cdr ls) (cons (deep-reverse (car ls)) acc));  If adding  a list, reverse it first
                (deep-reverse-2 (cdr ls) (cons (car ls) acc)))))
      (deep-reverse-2 ls '()))
    

    This version of deep-copy uses an accumulator and is tail recursive.

    This checks to see if the element is a list before adding it to the list, and if it is, reverses it first. Since it calls itself to revers the inner list, it can handle arbitrary nesting.

    (deep-reverse '(a (b c d) e)) -> '(e (d c b) a)
    

    which is in reverse alphabetical order, despite the fact that there is a nested list. It evaluates as so:

    (deep-reverse-2 '(a (b c d) e) '()); Which calls
    (deep-reverse-2 '((b c d) e)  '(a)); which is the same as
    (deep-reverse-2 '(e) (cons (deep-reverse-2 '(b c d) '()) '(a))); it calls deep-reverse on the list '(b c d) before consing it on.
    (deep-reverse-2 '(e) (cons (deep-reverse-2 '(c d)  '(b)) '(a)))
    (deep-reverse-2 '(e) (cons (deep-reverse-2 '(d)  '(c b)) '(a)))
    (deep-reverse-2 '(e) (cons '(d c b) '(a)))
    (deep-reverse-2 '(e)  '((d c b) a))
    (deep-reverse-2 '() '(e (d c b) a))
    '(e (d c b) a)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a list containing strings that look like this: 00x000s00g00wfds0d dkdf00sdf00sdfg00jk kf00dfjkd0sdl0sd0f0
Say I have a list that looks like this: ['item1', 'item2', 'item3', 'item4', 'item5',
Say I have a list of links with duplicate values as below: <a href=#>Book</a>
let's say i have list looking like this: <select name=name id=id> <option value=10176232332>David</option> <option
Say you have a list of dicts like this {'id': 1, 'other_value':5} So maybe;
say I have this list: li = [[0, 20, ar], [20, 40, asdasd], [50,
Say I have this list: li = [a, b, a, c, x, d, a,
Say I have a list of strings with possible repetitions. I am trying to
Say I have a list of member, each of which is a custom object:
Say I have a list of integers: List<int> myInts = new List<int>() {1,2,3,5,8,13,21}; I

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.