named let vs. fold pasted by wasamasa on Wed Jun 1 16:20:33 2016
(define (enclosing-rect rects) (when (null? rects) (abort (usage-error "RECTS must be a list of at least one rect" 'enclosing-rect))) (let loop ((x (rect-x (car rects))) (y (rect-y (car rects))) (w (rect-w (car rects))) (h (rect-h (car rects))) (rects (cdr rects))) (if (pair? rects) (let ((rect (car rects))) (loop (min (rect-x rect) x) (min (rect-y rect) y) (max (rect-w rect) w) (max (rect-h rect) h) (cdr rects))) (rect x y w h)))) (define (enclosing-rect rects) (when (null? rects) (abort (usage-error "RECTS must be a list of at least one rect" 'enclosing-rect))) (fold (lambda (current outer) (let ((x (min (rect-x current) (rect-x outer))) (y (min (rect-y current) (rect-y outer))) (w (max (rect-w current) (rect-w outer))) (h (max (rect-h current) (rect-h outer)))) (rect x y w h))) (car rects) (cdr rects)))
more readable added by wasamasa on Wed Jun 1 16:30:43 2016
(define (extend-bounding-box r1 r2) (let ((x (min (rect-x r1) (rect-x r2))) (y (min (rect-y r1) (rect-y r2))) (w (max (rect-w r1) (rect-w r2))) (h (max (rect-w r1) (rect-w r2)))) (rect x y w h))) (define (enclosing-rect rects) (when (null? rects) (abort (usage-error "RECTS must be a list of at least one rect" 'enclosing-rect))) (fold extend-bounding-box (car rects) (cdr rects)))