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?
In
minimum = head . sort, thesortwon’t be done fully, because it won’t be done upfront. Thesortwill only be done as much as needed to produce the very first element, demanded byhead.In e.g. mergesort, at first
nnumbers of the list will be compared pairwise, then the winners will be paired up and compared (n/2numbers), then the new winners (n/4), etc. In all,O(n)comparisons to produce the minimal element.The above code can be augmented to tag each number it produces with a number of comparisons that went into its production:
Running it for several list lengths we see that it is indeed
~ n: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:Here are the results for lists of various lengths,
The above shows empirical orders of growth for increasing lengths of list,
n, which are rapidly diminishing as is typically exhibited by~ n log ncomputations. See also this blog post. Here’s a quick correlation check: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.