;;; File: ;;; analysis-lab.scm ;;; Authors: ;;; Janet Davis ;;; Samuel A. Rebelsky ;;; Jerod Weinman ;;; YOUR NAME HERE ;;; Summary: ;;; Procedures for the lab on analyzing procedures ; +-----------------------------+----------------------------------------------- ; | Finding the Brightest Color | ; +-----------------------------+ ;;; Procedure: ;;; all-rgb? ;;; Parameters: ;;; lst, a list ;;; Purpose: ;;; Determines whether lst contains only RGB colors. ;;; Produces: ;;; okay?, a Boolean ;;; Preconditions: ;;; [No additional] ;;; Postconditions: ;;; If (rgb? (list-ref lst i)) fails to hold for some valid i between ;;; 0 and (- (length lst) 1), okay? is false (#f). ;;; Otherwise, okay? is true (#t) (define all-rgb? (lambda (lst) (or (null? lst) (and (rgb? (car lst)) (all-rgb? (cdr lst)))))) ;;; Procedure: ;;; rgb-brightness ;;; Parameters: ;;; color, an RGB color ;;; Purpose: ;;; Computes the brightness of color on a 0 (dark) to 100 (light) scale. ;;; Produces: ;;; b, an integer ;;; Preconditions: ;;; color is a valid RGB color. That is, each component is between ;;; 0 and 255, inclusive. ;;; Postconditions: ;;; If color1 is likely to be perceived as lighter than color2, ;;; then (brightness color1) > (brightness color2). (define rgb-brightness (lambda (color) (round (* 100 (/ (+ (* 0.30 (rgb-red color)) (* 0.59 (rgb-green color)) (* 0.11 (rgb-blue color))) 255))))) ;;; Procedure: ;;; rgb-brighter ;;; Parameters: ;;; color1, an RGB color. ;;; color2, an RGB color. ;;; Purpose: ;;; Find the brighter of color1 and color2. ;;; Produces: ;;; brighter, an RGB color. ;;; Preconditions: ;;; [No additional] ;;; Postconditions: ;;; brighter is either color1 or color2 ;;; (rgb-brightness brighter) >= (rgb-brightness color1) ;;; (rgb-brightness brighter) >= (rgb-brightness color2) (define rgb-brighter (lambda (color1 color2) (if (>= (rgb-brightness color1) (rgb-brightness color2)) color1 color2))) ;; Procedure: ;;; rgb-brighter? ;;; Parameters: ;;; color1, a color ;;; color2, a color ;;; Purpose: ;;; Determine if color1 is strictly brighter than color 2. ;;; Produces: ;;; brighter?, a Boolean ;;; Preconditions: ;;; [No additional preconditions.] ;;; Postconditions: ;;; If (rgb-brightness color1) > (rgb-brightness color2) ;;; then brighter is true (#t) ;;; Otherwise ;;; brighter is false (#f) (define rgb-brighter? (lambda (color1 color2) (> (rgb-brightness color1) (rgb-brightness color2)))) ;;; Procedure: ;;; rgb-brightest ;;; Parameters: ;;; colors, a list of RGB colors ;;; Purpose: ;;; Find the brightest color in the list. ;;; Produces: ;;; brightest, an RGB color. ;;; Preconditions: ;;; colors contains at least one element. ;;; Postconditions: ;;; For any color in colors, ;;; (rgb-brightness color) <= (rgb-brightness brightest) ;;; brightest is an element of colors. (define rgb-brightest-1 (lambda (colors) (cond ((null? (cdr colors)) (car colors)) ((rgb-brighter? (car colors) (rgb-brightest-1 (cdr colors))) (car colors)) (else (rgb-brightest-1 (cdr colors)))))) (define rgb-brightest-2 (lambda (colors) (if (null? (cdr colors)) (car colors) (rgb-brighter (car colors) (rgb-brightest-2 (cdr colors)))))) (define rgb-brightest-3 (lambda (colors) (let kernel ((brightest-so-far (car colors)) (remaining-colors (cdr colors))) (if (null? remaining-colors) brightest-so-far (kernel (rgb-brighter brightest-so-far (car remaining-colors)) (cdr remaining-colors)))))) (define rgb-brightest-4 (lambda (colors) (when (not (all-rgb? colors)) (error "rgb-brightest: expects a list of colors; received" colors)) (if (null? (cdr colors)) (car colors) (rgb-brighter (car colors) (rgb-brightest-4 (cdr colors)))))) ; +-----------------+----------------------------------------------------------- ; | List Procedures | ; +-----------------+ ;;; Procedure: ;;; list-append ;;; Parameters: ;;; front, a list of size n ;;; back, a list of size m ;;; Purpose: ;;; Put front and back together into a single list. ;;; Produces: ;;; appended, a list of size n+m. ;;; Preconditions: ;;; front is a list [Unverified] ;;; back is a list [Unverified] ;;; Postconditions: ;;; For all i, 0 <= i < n, ;;; (list-ref appended i) is (list-ref front i) ;;; For all i, <= i < n+m ;;;; (list-ref appended i) is (list-ref back (- i n)) (define list-append (lambda (front back) (if (null? front) back (cons (car front) (list-append (cdr front) back))))) (define list-reverse-1 (lambda (lst) (if (null? lst) null (list-append (list-reverse-1 (cdr lst)) (list (car lst)))))) (define list-reverse-2 (lambda (lst) (letrec ((kernel (lambda (reversed remaining) (if (null? remaining) reversed (kernel (cons (car remaining) reversed) (cdr remaining)))))) (kernel null lst))))