[PRL] Accumulator for container capacity

Ethan Aubin ethan.aubin at pobox.com
Thu Sep 22 15:20:43 EDT 2005


On Thu, Sep 22, 2005 at 01:19:44PM -0400, Karl Lieberherr wrote:
> Ethan showed me how to remove that double traversal of the intermediate
> structure (which is as long as the container contents).

Hi PRL, If you interested here's the code I sent to Karl. The 3 folds
from Mitch's check function (counting map) are fused together and the
intermediate lists are removed. Cheers -- ethan.aubin at pobox.com

  (define (check2 ac)

    ;; item -> (pair weight nviolations)
    (define (weight-and-violations-of-item it)
      (cond
        ((Simple? it) (cons (Simple-weight it) 0))
        ((Container? it)
         (weight-and-violations-of-container it))))

    ;; container -> (pair weight nviolations)
    (define (weight-and-violations-of-container ac)
      (let ((tw&tv
	     (foldr (lambda (item tw&vs)
		      (let* ((wv (weight-and-violations-of-item item))
			     (weight (car wv))
			     (viols (cdr wv)))
			(cons (+ weight (car tw&vs))
			      (+ viols (cdr tw&vs)))))
		    (cons 0 0)
		    (Container-contents ac))))

        (let ((total-weight (car tw&tv))
              (total-viols  (cdr tw&tv)))
          (cons
            total-weight
            (if (> total-weight (Container-capacity ac))
              (+ 1 total-viols)
              total-viols)))))
    (cdr (weight-and-violations-of-container ac)))  



More information about the PRL mailing list