Challenge 3: Why [H|T] Failing Against [] Parallels Haskell's Own Base Case — Possible Solution ==================================================================== // haskell1-4's own recursive functions (like sumList) require TWO // separate equations to handle every possible list: a base case // specifically for the empty list ([]), and a recursive case // specifically for a non-empty list ((x:xs)). The recursive case's // own pattern, (x:xs), simply does NOT match an empty list at all -- // attempting to apply it to [] fails to match, which is exactly why // the separate [] equation has to exist as its own case. // // Prolog's [H|T] pattern behaves identically at the level of // unification: [H|T] = [] genuinely FAILS -- there is no way to make // an empty list identical to "some head consed onto some tail," since // an empty list has no head or tail to bind H and T to at all. Just // like Haskell's (x:xs) pattern, [H|T] structurally REQUIRES at least // one element to succeed. // // This is exactly why a recursive Prolog predicate over a list -- the // kind covered in the next chapter -- also needs two separate clauses, // mirroring Haskell's own base-case/recursive-case shape precisely: // // mySum([], 0). // mySum([H|T], Sum) :- mySum(T, RestSum), Sum is H + RestSum. // // The first clause is the base case, matching only the empty list. // The second clause's own [H|T] pattern can never match [] on its // own -- exactly like (x:xs) in Haskell -- so the base case has to // exist separately to cover that one case the recursive clause // structurally cannot. WHY THIS WORKS AS AN ANSWER ------------------------------ This correctly identifies that [H|T] and (x:xs) both structurally require at least one element to match, meaning both languages need a genuinely separate base-case clause/equation for the empty-list case, directly tying the parallel back to haskell1-4's own recursive function shape as the chapter's warn-box requests.