Half way down here...
https://en.wikibooks.org/wiki/Haskell/Laziness
...is an exercise asking about the effects of changes to an alternative implementation of the head
function that uses irrefutable patterns. It provides a definition of head'
as follows, noting that it will always return undefined
due to irrefutable matching of the first equation:
head' :: [a] -> a
head' ~[] = undefined
head' ~(x:xs) = x
Then it asks:
- Why won't changing the order of the equations to
head'
help here? - If the first equation is changed to use an ordinary refutable pattern,
will the behavior of
head'
still be different from that ofhead
? If so, how?
In GHC 7.8.4 it seems that changing the order "helps" at least to the extent of making this function behave like the regular partial version of head
, albeit with a different exception in the empty list case. The answer to the second question would appear to me to be "no", but given the "if so, how" addendum, it feels like I must be missing something here too. Can anyone enlighten me? The solutions link on the page does not cover this exercise unfortunately.