To add up all the elements in a list:
> ((nested-reduce + 0) '(1 2 3 4)) 10Note 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.
|
Variation on tree example by Mike O'Donnell, 2001, Nov 19
to: