Categories
Projects

Minara

Sometimes it’s easiest to so something recursively:

(define (combine-ranges a b)
(cons (min (car a) (car b))
(max (car a) (cdr b))))

(define (combine-selection-ranges-aux range ranges sorted)
(if (ranges)
(let ((next-range (car ranges))
(rest (cdr ranges)))
(if (selections-overlap? range next-range)
(combine-selection-ranges-aux (car rest)
(cdr rest)
(cons (combine-ranges range
next-range)
sorted)))
(combine-selection-ranges-aux next-range rest
(cons range sorted))))
(cons range sorted))

(define (combine-selection-ranges ranges)
(combine-selection-ranges-aux (car ranges) (cdr ranges)))

But I haven’t debugged this yet and I think I may have missed a case. 🙂