Exercise 3.29: Refactored fold and implementations.