Feedback Sum all elements in a tree or list 

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)
Date: 2001, Nov 18
From: <gckettle@midway.uchicago.edu>

To add up all the elements in a list:
> ((nested-reduce + 0) '(1 2 3 4))

10
Note that the 0 is added in the end.

Here's the same thing for a tree:

>((reduce-tree + (lambda (x) 
                    (cond (null? x)
                          0
                          (else x))))
   (cons (cons 1 2) (cons 3 4)))

10
Here, instead of just a 0 on the end we need a function. In this case, it simply returns the value of the leaf, or returns 0 if it's nil.


Messages

1. More Variation on tree example by Mike O'Donnell, 2001, Nov 19

to: "Sum all elements in a tree or list"