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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:34:16+00:00 2026-06-08T18:34:16+00:00

I come from a C++ background so I’m not sure if I’m even going

  • 0

I come from a C++ background so I’m not sure if I’m even going about this properly. But what I’m trying to do is write up quick sort but fallback to insertion sort if the length of a list is less than a certain threshold. So far I have this code:

insertionSort :: (Ord a) => [a] -> [a]
insertionSort [] = []
insertionSort (x:xs) = insert x (insertionSort xs)

quickSort :: (Ord a) => [a] -> [a]
quickSort x = qsHelper x (length x)

qsHelper :: (Ord a) => [a] -> Int -> [a]
qsHelper [] _ = []
qsHelper (x:xs) n 
    | n <= 10 = insertionSort xs
    | otherwise =  qsHelper before (length before) ++ [x] ++ qsHelper after (length after)
        where
            before = [a | a <- xs, a < x]
            after = [a | a <- xs, a >= x]

Now what I’m concerned about is calculating the length of each list every time. I don’t fully understand how Haskell optimizes things or the complete effects of lazy evaluation on code like the above. But it seems like calculating the length of the list for each before and after list comprehension is not a good thing? Is there a way for you to extract the number of matches that occurred in a list comprehension while performing the list comprehension?

I.e. if we had [x | x <- [1,2,3,4,5], x > 3] (which results in [4,5]) could I get the count of [4,5] without using a call to length?

Thanks for any help/explanations!

  • 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-08T18:34:17+00:00Added an answer on June 8, 2026 at 6:34 pm

    Short answer: no.

    Less short answer: yes, you can fake it. import Data.Monoid, then

        | otherwise =  qsHelper before lenBefore ++ [x] ++ qsHelper after lenAfter
            where
                (before, Sum lenBefore) = mconcat [([a], Sum 1) | a <- xs, a < x]
                (after, Sum lenAfter) = mconcat [([a], Sum 1) | a <- xs, a >= x]
    

    Better answer: you don’t want to.

    Common reasons to avoid length include:

    • its running time is O(N)
      • but it costs us O(N) to build the list anyway
    • it forces the list spine to be strict
      • but we’re sorting the list: we have to (at least partially) evaluate each element in order to know which is the minimum; the list spine is already forced to be strict
    • if you don’t care how long the list is, just whether it’s shorter/longer than another list or a threshold, length is wasteful: it will walk all the way to the end of the list regardless
      • BINGO
    isLongerThan :: Int -> [a] -> Bool
    isLongerThan _ []     = False
    isLongerThan 0 _      = True
    isLongerThan n (_:xs) = isLongerThan (n-1) xs
    
    quickSort :: (Ord a) => [a] -> [a]
    quickSort []     = []
    quickSort (x:xs) 
        | not (isLongerThan 10 (x:xs)) = insertionSort xs
        | otherwise =  quickSort before ++ [x] ++ quickSort after
            where
                before = [a | a <- xs, a < x]
                after = [a | a <- xs, a >= x]
    

    The real inefficiency here though is in before and after. They both step through the entire list, comparing each element against x. So we are stepping through xs twice, and comparing each element against x twice. We only have to do it once.

                (before, after) = partition (< x) xs
    

    partition is in Data.List.

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

Sidebar

Related Questions

I come from a c# background where everything has its own namespace, but this
I come from Java Background and so used to Debugging using Eclipse but have
I come from a C++ background and I've been working with C# for about
I come from frontend background, so I apologize if this db question seems dumb.
I come from a Java background. I am wanting to learn more about concurrency
I come from MySQL background and I am trying to learn MongoDB. I have
I come from a java background, but I'm working on a django-based website. To
I come from SML background and feel quite comfortable with high-order functions. But I
I come from a Java background and I am now trying to study PHP
I come from a Java background, where packages are used, not namespaces. I'm used

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.