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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:28:48+00:00 2026-05-30T06:28:48+00:00

I’m writing a function that merges two lists. If one of the list’s item

  • 0

I’m writing a function that merges two lists. If one of the list’s item is a list, I need to extract it too.

list1:'(1 (2 3 4) 5 6)) 

list2: '(7 (8 9) 10 (11))

output: (1 2 3 4 5 6 7 8 9 10 11)

I tried to solve it by my code doesn’t work. What is the problem?

(define (merge lis1 lis2)
    (define (combine lis fine)
        (cond
            ((null? lis) lis)
            ((list? lis) (combine (car lis) fine) (combine (cdr lis) fine))
            (else  (cons lis fine))))

        (cond
            (combine (cons lis2 lis1) '())))
  • 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-30T06:28:50+00:00Added an answer on May 30, 2026 at 6:28 am

    The easiest way is to simply use the library function flatten.

    (define (merge lis1 lis2)
      (flatten (cons lis1 lis2)))
    

    flatten takes a list that can contain lists (who in turn can contain more lists, …) and flattens the result into a list of non-lists, which is what your combine function seems to be trying to do.

    (flatten '(1 2 (3 (4 5) 6))) returns '(1 2 3 4 5 6)

    If this library function is off limits, your code is actually pretty close to correct.

    The first issue is on the ((list? lis) (combine (car lis) fine) (combine (cdr lis) fine) ) line. fine is never changed, so the code evaluates (combine (car lis) fine) and then returns (combine (cdr lis) fine), where the fine in the second expression is the original value of fine. This line is the same thing as ((list? lis) (combine (cdr lis) fine) ), which is obviously not what we want. Instead, we have to use the first expression inside the second expression ((list? lis) (combine (cdr lis) (combine (car lis) fine))).

    The second issue is that, in combine, when lis is null, we need to return fine, not lis.

    The next issue is that this code goes through the list, takes the first element of lis and puts it on the front of fine, and then passes the newly created list and uses it for the next iteration of the function, where it takes second value in lis and sticks it on the front of the new fine, in front of the first value of lis. The results in the order of the return value of fine being reversed — (merge '(1 2 (3)) '(4 (5 6))) will return (6 5 4 3 2 1). We have two choices: we can call reverse on the return from combine in the body of merge or we can invert the line we changed above, making it ((list? lis) (combine (car lis) (combine (cdr lis) fine))). This means that we would add the rest of the list to fine before adding the current element, which is what we want.

    Another issue is that we need to cons lis1 to lis2, not the other way around.

    The final issue is that the cond in the body of merge is unnecessary — we can remove it.

    As a side note, it is generally considered neater to not give close parentheses a new line and indent the body of a define or cond by just two spaces.

    With all of these changes, the final code is:

    (define (merge lis1 lis2)
      (define (combine lis fine)
        (cond
          ((null? lis) fine)
          ((list? lis) (combine (car lis)
                                (combine (cdr lis) fine)))
          (else  (cons lis fine))))
    
      (combine (cons lis1 lis2) '()))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping 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.