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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:49:08+00:00 2026-05-30T21:49:08+00:00

I am learning haskell and the function definition I see is: quickSort (x :

  • 0

I am learning haskell and the function definition I see is:

quickSort (x : xs) = (quickSort less) ++ (x : equal) ++ (quickSort more)
                 where less = filter (< x) xs
                       equal = filter (== x) xs
                       more = filter (> x) xs

Is it possible to write it with only one traversal of the list, instead of 3?

  • 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-30T21:49:10+00:00Added an answer on May 30, 2026 at 9:49 pm

    Although late, here’s a version that’s supposed to not leak space as much (and seems to run about twice faster than the other 3-way version here):

    qsort3 xs = go xs [] 
      where
        go     (x:xs) zs       = part x xs zs [] [] []
        go     []     zs       = zs
        part x []     zs a b c = go a ((x : b) ++ go c zs)
        part x (y:ys) zs a b c =
            case compare y x of
                      LT -> part x ys zs (y:a) b c
                      EQ -> part x ys zs a (y:b) c
                      GT -> part x ys zs a b (y:c)
    

    This addresses the possible problem with using tuples, where let (a,b) = ... is actually translated into let t= ...; a=fst t; b=snd t which leads to the situation where even after a has been consumed and processed, it is still kept around alive, as part of the tuple t, for b to be read from it – though of course completely unnecessary. This is known as “Wadler pair space leak” problem. Or maybe GHC (with -O2) is smarter than that. 🙂

    Also this apparently uses difference lists approach (thanks, hammar) which also makes it a bit more efficient (about twice faster than the version using tuples). I think part uses accumulator parameters, as it builds them in reversed order.

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

Sidebar

Related Questions

I am currently learning Haskell. I am trying to write a function which given
I'm learning Haskell and I'd like to write some multithreaded programs now to see
I'm learning Haskell and I'm trying to write a function to return a list
Learning Haskell is killing me. If I am going to write a function that
As I am learning Haskell, I see that there is a lot of language
I'm still learning Haskell, and I was wondering if there is a less verbose
I have started learning Haskell from Learn You a Haskell . In one of
I'm just learning Haskell and I am trying to write some code that simply
Learning haskell and want a function to generate a 2D grid similar to how
I am learning Haskell. I have created function which returns multiplication table up to

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.