CS 2420-20 Homework 17

Due: Friday, March 18th, 2011 9:10am

Part 1 – Other Recursion

Convert the following sum-widget function (which sums the numbers in a widget) to tail form:

  ;; A widget is either
  ;;  - num
  ;;  - (make-single num widget)
  ;;  - (make-double num widget widget)
  (define-struct single (v rest))
  (define-struct double (v left right))
  (define (sum-widget w)
    (match w
      [(? number?) w]
      [(single v rest) (+ v (sum-widget rest))]
      [(double v left right) (+ v
                                (sum-widget left)
                                (sum-widget right))]))

Last update: Thursday, April 7th, 2011