; Return half the length of the list L. (define (half-length L) (truncate (/ (length L) 2)) ) ; Determine if x is a possible median of the values list, ; a list of numbers. It is assumed that x is an element of values. (define (possible-median? x values) (= (how-many-< x values) (half-length values)) ) ; Find the median of the values list, a list of numbers. ; *** NOTE: This function does not work correctly when values ; contains duplicate numbers. *** (define (median values) (find-if (lambda (x) (possible-median? x values)) values)) ; Return the list of numbers in the values list that are smaller than x. ; x is a number, values is a list of numbers. (define (smaller-vals x values) (keep-if (lambda (y) (< y x)) values)) ; Return how many numbers in the values list are smaller than x. ; x is a number, values is a list of numbers. (define (how-many-< x values) (length (smaller-vals x values)) )