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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:35:37+00:00 2026-06-10T00:35:37+00:00

I was looking around stackoverflow Non-Trivial Lazy Evaluation , which led me to Keegan

  • 0

I was looking around stackoverflow Non-Trivial Lazy Evaluation, which led me to Keegan McAllister’s presentation: Why learn Haskell. In slide 8, he shows the minimum function, defined as:

minimum = head . sort

and states that its complexity is O(n). I don’t understand why the complexity is said to be linear if sorting by replacement is O(nlog n). The sorting referred in the post can’t be linear, as it does not assume anything about the data, as it would be required by linear sorting methods, such as counting sort.

Is lazy evaluation playing a mysterious role in here? If so, what is the explanation behind it?

  • 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-10T00:35:38+00:00Added an answer on June 10, 2026 at 12:35 am

    In minimum = head . sort, the sort won’t be done fully, because it won’t be done upfront. The sort will only be done as much as needed to produce the very first element, demanded by head.

    In e.g. mergesort, at first n numbers of the list will be compared pairwise, then the winners will be paired up and compared (n/2 numbers), then the new winners (n/4), etc. In all, O(n) comparisons to produce the minimal element.

    mergesortBy less [] = []
    mergesortBy less xs = head $ until (null.tail) pairs [[x] | x <- xs]
      where
        pairs (x:y:t) = merge x y : pairs t
        pairs xs      = xs
        merge (x:xs) (y:ys) | less y x  = y : merge (x:xs) ys
                            | otherwise = x : merge  xs (y:ys)
        merge  xs     []                = xs
        merge  []     ys                = ys
    

    The above code can be augmented to tag each number it produces with a number of comparisons that went into its production:

    mgsort xs = go $ map ((,) 0) xs  where
      go [] = []
      go xs = head $ until (null.tail) pairs [[x] | x <- xs]   where
        ....
        merge ((a,b):xs) ((c,d):ys) 
                | (d < b)   = (a+c+1,d) : merge ((a+1,b):xs) ys    -- cumulative
                | otherwise = (a+c+1,b) : merge  xs ((c+1,d):ys)   --   cost
        ....
    
    g n = concat [[a,b] | (a,b) <- zip [1,3..n] [n,n-2..1]]   -- a little scrambler
    

    Running it for several list lengths we see that it is indeed ~ n:

    *Main> map (fst . head . mgsort . g) [10, 20, 40, 80, 160, 1600]
    [9,19,39,79,159,1599]
    

    To see whether the sorting code itself is ~ n log n, we change it so that each produced number carries along just its own cost, and the total cost is then found by summation over the whole sorted list:

        merge ((a,b):xs) ((c,d):ys) 
                | (d < b)   = (c+1,d) : merge ((a+1,b):xs) ys      -- individual
                | otherwise = (a+1,b) : merge  xs ((c+1,d):ys)     --   cost
    

    Here are the results for lists of various lengths,

    *Main> let xs = map (sum . map fst . mgsort . g) [20, 40, 80, 160, 320, 640]
    [138,342,810,1866,4218,9402]
    
    *Main> map (logBase 2) $ zipWith (/) (tail xs) xs
    [1.309328,1.2439256,1.2039552,1.1766101,1.1564085]
    

    The above shows empirical orders of growth for increasing lengths of list, n, which are rapidly diminishing as is typically exhibited by ~ n log n computations. See also this blog post. Here’s a quick correlation check:

    *Main> let xs = [n*log n | n<- [20, 40, 80, 160, 320, 640]] in 
                                        map (logBase 2) $ zipWith (/) (tail xs) xs
    [1.3002739,1.2484156,1.211859,1.1846942,1.1637106]
    

    edit: Lazy evaluation can metaphorically be seen as kind of producer/consumer idiom1, with independent memoizing storage as an intermediary. Any productive definition we write, defines a producer which will produce its output, bit by bit, as and when demanded by its consumer(s) – but not sooner. Whatever is produced is memoized, so that if another consumer consumes same output at different pace, it accesses same storage, filled previously.

    When no more consumers remain that refer to a piece of storage, it gets garbage collected. Sometimes with optimizations compiler is able to do away with the intermediate storage completely, cutting the middle man out.

    1 see also: Simple Generators v. Lazy Evaluation by Oleg Kiselyov, Simon Peyton-Jones and Amr Sabry.

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

Sidebar

Related Questions

I have been looking around ocn Google and Stackoverflow but haven't found what I
Been looking around stackoverflow and google for ways to solve this issue im having
Hi StackOverflow friends. Running Drupal 6.22 php 5.3 I have been looking around now
I have been looking around Google and Stackoverflow, but I have yet to find
I have been looking around stackoverflow for automated GUI tools for testing our web
I was looking around for jQuery grid recommendations and came across this question/answers: https://stackoverflow.com/questions/159025/jquery-grid-recommendations
Been looking around google and StackOverflow, everybody practically have the same advice to do:
Looking around, I can't name a single web application (not web service) that uses
Been looking around and can't find a definite way to do this... I need
After looking around (for not terribly long I have to admit) I wonder if

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.