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

  • Home
  • SEARCH
  • 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 8640331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:15:14+00:00 2026-06-12T11:15:14+00:00

I have no problem combining the lists but sorting them in ascending order is

  • 0

I have no problem combining the lists but sorting them in ascending order is where i am struggling.

 (define (combineASC l1 l2)
   (cond 
     ((null? l1) l2)
     (#t (cons (car l1) (combineASC (cdr l1) l2)))
     (sort l2 #'<))) ; I found this part in an online source

This is the output i get:

(combineASC '(2 4 6) '(1 4 5))

(2 4 6 1 4 5)

Does anyone have any suggestions for me?

  • 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-12T11:15:15+00:00Added an answer on June 12, 2026 at 11:15 am

    So you’re combining two input lists, each already sorted in ascending order. You want to “weave” them into one, also in ascending order.

    For that, you just take both head elements (each from each input list) and compare; then you take the smallest out from its list, and combine further what you’re left with – using same function.

    There will be no sorting involved. The resulting list will be already sorted, by virtues of the process that defines it.

    This operation is commonly called “merge”. It preserves duplicates. Its duplicates-removing counterpart, merging two ordered lists into one as well, is known a “union”. That is because these ordered (non-descending, or strictly ascending) lists can be seen as representation of sets.


    Another subtlety to take note of is, what to do when the two head elements are equal. We’ve already decided to preserve the duplicates, yes, but which of the two to take out first?

    Normally, it’s the left one. Then when such defined merge operation is used as part of merge sort, the sort will be stable (of course the partitioning has to be defined properly for that too). Stable means, the original order of elements is preserved.

    For example, if the sort is stable, when sorting by the first element of pairs, (3,1) (1,2) (3,3) is guaranteed to be sorted as (1,2) (3,1) (3,3) and not as (1,2) (3,3) (3,1).

    So, following the skeleton of your code, we get

    ;; combine two non-decreasing lists into one non-decreasing list,
    ;; preserving the duplicates
    (define (combineNONDECR l1 l2)
       (cond 
         ((null? l1) l2)
         ((null? l2) l1)
         ((<= (car l1) (car l2))
          (cons (car l1) (combineNONDECR (cdr l1) l2)))
         (t
          (cons (car l2) (combineNONDECR l1 (cdr l2))))))
    

    But if you really need the result to be in ascending order, as opposed to non-decreasing, then you’d have to tweak this a little bit – make the = case separate, and handle it separately, to stop the duplicates from creeping in (there are no duplicates in each of the ascending-order lists, but the lists might contain some duplicates between the two of them).

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

Sidebar

Related Questions

I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a problem with combining or calculating common/equal part of these two dictionaries.
I have a problem with compiling an Oracle trigger via SQL*PLUS - I don't
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have problem with show or hide form in Window Form Application. I start
I have problem with UIWebView delay when the load image from url. In my
I have problem while loading data into html select when users press or click
I have problem developing with live555. I already build the lib-files and example projects

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.