Haskell/Solutions/Laziness


 * 1) Because   needs to evaluate   to convert it to a.
 * 2)   is stricter: it must walk through the whole list to discover its length, even if it does not need to examine the single values.

Instead, to evaluate, when   evaluates the passed list it obtains  , where thunk_0 represents the result of. Thus, you can verify that  (actually,   is equivalent to  ) while   fails. Also note that, if  is a valid   list (which does not contain   among its elements), all following expressions fail to evaluate: g (1:undefined) => length undefined => undefined

whereas the following evaluate successfully: g (1:undefined:ls) => length (undefined:ls) => 1 + length ls g (undefined:ls) => length ls

The difference between  and  may appear confusing, but is correct, because in the first case   replaces a list (in particular, the tail of the cons cell passed to  ), while in the second case   replaces a list value (which is not needed).