<div dir="ltr">Hi Rohan,<div><br></div><div>The definition of Prelude.head is</div><div><br></div><div>head :: [a] -> a</div><div>head (x:_) = x</div><div>head [] = undefined -- not exactly true but the details are irrelevant here</div><div><br></div><div>This is the same as</div><div><br></div><div>head :: [a] -> a</div><div>head xs = case xs of</div><div>                   (x : xs) -> x</div><div>                   [] -> undefined</div><div><br></div><div>(Remeber that a list type can be thought of as data [a] = x : xs | [], hence (:) and [] are both data constructors.)</div><div><br></div><div>A case expression forces the evaluation of the scrutinee (xs in this case) and since xs = ([1..] ++ [10]), the expression is forced to evaluate to Weak Head Normal Form, which means it evaluates until it hits a data constructor or function. So this requires us to lookup the definition of (++) as well.</div><div><br></div><div><div>(++) []     ys = ys</div><div>(++) (x:xs) ys = x : xs ++ ys<br></div></div><div><br></div><div>This is the same as</div><div><br></div><div>(++) xs ys = case xs of</div><div>                  [] -> ys </div><div>                  x:xs -> x : (xs ++ ys)</div><div><br></div><div>Hence, evaluting ([1..] ++ [10]) will give 1 : ([2..] + [10]) which is in WHNF, hence head ([1..] ++ [10]) = 1.</div><div><br></div><div>So yes, you are correct, adding the (++) won't add the list until the evaluation "gets there", it will be stored as a thunk (suspended state). I suppose you can effectively consider that as "adding in constant time". But do note that it will consume quite a bit of memory over time to store the appends and the singleton lists.</div><div><br></div><div>Yes, list concatenation is O(n), but pushing to the end of the queue is not due to nature of laziness. This is precisely why it's hard to do running time analysis in the context of laziness. </div><div><br></div><div>But due note that in your particular example, appending to [1..] is futile since it's an infinite list, so that 10 will never actually get "seen" no matter how far you go in the list.</div><div><br></div><div>Hope that helps!</div><div>Rahul Muttineni</div></div>