Warning Defining reduce from reduce-tree is subtle with nested lists 

Forum: U Chicago, Com Sci 221, autumn 2000
Re: News Assignment #7, due Monday 20 November (Mike O'Donnell)
Date: 2000, Nov 19
From: Mike O'Donnell <odonnell@cs.uchicago.edu>

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.


Messages

1. More Then there's the grandmother of all tree recursions by Mike O'Donnell, 2000, Nov 19

to: "Defining reduce from reduce-tree is subtle with nested lists"