Challenge 2: Summing a Longer Expression List — Possible Solution ==================================================================== expr_demo.pl: expr(Sum) --> term(T), expr_rest(T, Sum). expr_rest(Acc, Sum) --> [plus], term(T), { Acc1 is Acc + T }, expr_rest(Acc1, Sum). expr_rest(Sum, Sum) --> []. term(T) --> [T], { number(T) }. Query: ?- phrase(expr(Sum), [5, plus, 10, plus, 15, plus, 20]). Sum = 50. Explanation: expr/1 first matches a single term (5), then hands off to expr_rest(5, Sum) to consume the rest of the list. expr_rest recursively matches [plus], term(T) three more times -- once for each remaining plus/number pair -- computing a running total via { Acc1 is Acc + T } at each step: 5+10=15, then 15+15=30, then 30+20=50. Once the input list is fully consumed, expr_rest's second clause (expr_rest(Sum, Sum) --> [].) matches the empty remaining input and unifies the final accumulator directly with Sum, giving the correct total of 50 regardless of how many plus-separated terms the list actually contains. WHY THIS WORKS AS AN ANSWER ------------------------------ This runs the chapter's own grammar unchanged against a longer, four-term input list, confirming the recursive accumulator pattern in expr_rest/2 correctly scales beyond the chapter's original three-term example without any modification to the grammar itself.