More Variation on tree example 

Forum: Com Sci 221, U Chicago, Autumn 2001, Old Classwork
Re: News Assignment #7 due Monday, 19 November (Mike O'Donnell)
Re: Question nested-reduce, reduce-tree examples? (Avi Schwab)
Re: Feedback Sum all elements in a tree or list
Date: 2001, Nov 19
From: Mike O'Donnell <odonnell@cs.uchicago.edu>

Both examples make sense, and they do the same thing. But the tree example isn't quite in the spirit of general trees. In that spirit, it makes more sense to suppose that all leaves must be numbers, instead of treating nil as a synonym for 0. So you can sum a tree of numbers with the even simpler code

>((reduce-tree + (lambda (x) x))

   (cons (cons 1 2) (cons 3 4)))

10
Of course, when you use reduce-tree to reimplement nested-reduce, you need precisely the sort of conditional treatment of nil in Mr. Kettler's example, since now your code must deal with tree encodings of lists, rather than trees for their own sakes.




Mike O'D.


Messages

to: "Variation on tree example"