CS 2420-20 Homework 16

Due: Thursday, March 17th, 2011 9:10am

Part 1 – List Recursion

Convert the following sum function (which sums numbers pairwise from two same-length lists, producing a new list) to tail form:

  (define (sum a b)
    (cond
      [(empty? a) empty]
      [else (cons (+ (first a) (first b))
                  (sum (rest a) (rest b)))]))

Part 2 – Tree Recursion

Convert the following sum-trees function (which sums numbers pairwise from two same-shaped trees, producing a new tree) to tail form:

  (define (sum-trees a b)
    (cond
      [(empty? a) empty]
      [else (make-tree
             (sum-trees (tree-left a) (tree-left b))
             (+ (tree-val a) (tree-val b))
             (sum-trees (tree-right a) (tree-right b)))]))

Last update: Thursday, April 7th, 2011
mflatt@cs.utah.edu