|
When I made up problem #2, I was only thinking of unnested lists of atomic symbols---e.g, (1 2 3), (a 2 b 3 c 5), but not (1 (2 3) 4), (1 () a () 3). The problem is a bit subtle in the case of nested lists, because reduce-tree produces functions that work recursively through the sublists, while reduce produces functions that only work recursively through the outermost list. I will give full credit for a solution that only works on unnested lists of atoms. I'll award some extra credit for a truly full solution.
The full solution is not very complicated (less than twice the code of the unnested solution), but it's a bit subtle. You must work with a helper function that produces both the result of (reduce f v) on an input list, and the unreduced input list, then select which one you need depending on the computational context. Some of you who are more familiar with Scheme may think of a simpler solution that works by counting the levels of recursion with assignments to a global variable. Please don't submit that solution. It's implicit in this part of the course that we are only interested in purely functional programs, and the level-counting method is not purely functional.
Mike O'D.
|