Cells versus square values

A pure lazy program defines the positive integer squares as follows. The notation [1..] denotes the unbounded list starting at 1; map applies its function lazily to the elements.


haskell
squares = map (\n -> n * n) [1..]
a = sum (take 3 squares)
b = head (drop 4 squares)
c = sum (take 6 squares)


Here, take k selects the first k elements, drop k skips the first k, and head requests the next element. Traversing the list spine does not itself demand skipped numeric elements.


Each row starts a fresh evaluation with no previously cached squares. Assume no compiler transformations. Complete the table with the final result and the number of evaluations of n * n. Enter exact integers.

Requested runResultMultiplication evaluations
a
b
c

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Lazy Evaluation Questions: