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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:06:59+00:00 2026-06-02T15:06:59+00:00

I need to create a program that will reverse a list destructively. For example

  • 0

I need to create a program that will reverse a list destructively. For example lets say..

scm> (define L (list 1 2 3 4))
scm> (reverse! L)
(4 3 2 1)
scm> L
(1)

Where L becomes the last element of the reversed list. I know I am supposed to use set-cdr! somehow but cannot figure out how to implement it.

  • 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-06-02T15:07:04+00:00Added an answer on June 2, 2026 at 3:07 pm

    Because this looks like homework, I can’t give you a straight answer. I’ll show you the general structure of the solution, so you can figure out the details and fill-in the blanks:

    (define (reverse! lst)
      (let loop ((lst lst)
                 (acc '()))
        (if (null? lst)
            acc
            (let ((tail <?1?>))
              (set-cdr! <?2?> <?3?>)
              (loop tail lst)))))
    
    (define lst (list 1 2 3 4))
    lst
    > (1 2 3 4)
    
    (reverse! lst)
    > (4 3 2 1)
    
    lst
    > (1)
    

    In the above code:

    • The original list is traversed using a named let for simplicity, given that another parameter is needed
    • A new acc parameter is defined, to serve as the accumulator for the reversed list
    • When the recursion ends, the answer in the accumulator is returned

    Now, for the recursive step:

    • In <?1?> we need to obtain a reference to the rest of the list and save it, given that we’re going to modify it
    • The key point lies in the line (set-cdr! <?2?> <?3?>). You’ll have to set the next element of the current list to the previously accumulated, reversed list
    • Finally, the recursion proceeds with the new accumulated values

    Notice that in the end, the lst reference got modified in-place and now is pointing to the last element of the list. If you need lst to point to the reversed list, then simply do this:

    (define lst (list 1 2 3 4))
    lst
    > (1 2 3 4)
    
    (set! lst (reverse! lst))
    lst
    > (4 3 2 1)
    

    The procedure described reverses a list destructively and doesn’t create a new list (no cons operations are used.)

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

Sidebar

Related Questions

I need to create a program in c# code that will sleep my machine
We need to create a program in PHP/MySQL that will display all the 'people',
I need to create a program that will communicate with other programs on the
I need to create a C program that will copy one folder content to
I need to create an installer program that will do install the following: ASP.Net
I need to create a C++ dll that will be called from another program
I need to create program that creates a XML schema like below using System.Xml.XmlSchema
I want to create a program that produces an executable slideshow. So I need
I'm needing to write a program (C#) that will allow the user to create
For my work, I need to create a service that will detect an abnormal

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.