In the following generalized code:
nat = [1..xmax]
xmax = *insert arbitrary Integral value here*
setA = [2*x | x <- nat]
setB = [3*x | x <- nat]
setC = [4*x | x <- nat]
setD = [5*x | x <- nat]
setOne = setA `f` setB
setTwo = setC `f` setD
setAll = setOne ++ setTwo
setAllSorted = quicksort setAll
(please note that ‘f’ stands for a function of type
f :: Integral a => [a] -> [a] -> [a]
that is not simply ++)
how does Haskell handle attempting to print setAllSorted?
does it get the values for setA and setB, compute setOne and then only keep the values for setOne in memory (before computing everything else)?
Or does Haskell keep everything in memory until having gotten the value for setAllSorted?
If the latter is the case then how would I specify (using main, do functions and all that other IO stuff) that it do the former instead?
Can I tell the program in which order to compute and garbage collect? If so, how would I do that?
The head of
setAllSortedis necessarily less-than-or-equal to every element in the tail. Therefore, in order to determine the head, all ofsetOneandsetTwomust be computed. Furthermore, since all of the sets are constant applicative forms, I believe they will not be garbage collected after being computed. The numbers themselves will likely be shared between the sets, but the cons nodes that glue them together will likely not be (your luck with some will depend upon the definition off).