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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:47:55+00:00 2026-06-14T12:47:55+00:00

In a practice exam that I’m taking, there is a question that asks to

  • 0

In a practice exam that I’m taking, there is a question that asks to create a procedure that takes a list and creates a new list that contains two of each element in the old list while preserving the order. The example they provide:

    (double-duplicate (list 1 2 3 4 4 5))

produces

    (1 1 2 2 3 3 4 4 4 4 5 5)

I managed to find a solution that uses map and flatten:

    (define (flatten list)
       (cond ((null? list) '())
             ((list? (car list)) (append (flatten (car list)) (flatten (cdr list))))
             (else (cons (car list) (flatten (cdr list))))))

    (define (double-duplicate ls)
      (define (helper list1 list2)
        (flatten (map list list1 list2)))
      (helper ls ls))

while it does work, I do not feel that it is the most effective solution since I am using the form of map that takes 3 parameters and I do not like the idea of having to write a second procedure (flatten) just to get rid of the excess parentheses. Can anyone think of a better way to doing this? I’m a bit lost as to how else I can write it. I appreciate any ideas.

*Note: I am using MIT scheme for all this.

  • 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-14T12:47:56+00:00Added an answer on June 14, 2026 at 12:47 pm

    Are you using Racket? If so, you can use append-map:

    (define (double-duplicate lst)
      (append-map (lambda (x) (list x x)) lst))
    

    append-map is analogous to the likes of Scala’s flatmap.

    It’s not hard to write your own version of append-map if you need to:

    (define (append-map func lst)
      (fold-right (lambda (e r)
                    (append (func e) r)) '() lst))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, this is another question taken from this practice exam that was given to
I have a practice exam for my compilers course with the following questions that
Is there a best practice for making an entity immutable? Users create exercises in
Well, this code was taken from a practice exam for a programming thing that
Taking a practice exam the exam said I got this one wrong. The answer
I'm taking a practice test for the exam 70-536. Below is a screenshot. The
for practice I am writing an activity that takes a string from a local
My teacher gave out a practice exam on java recently and I'm curious to
I've a best practice question on CouchDB (actually I'm using TouchDB a CouchDB port
I'm studying for an Algorithms exam and one of the practice problems is a

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.