;; Use the first argument to decide in which direction to look first: (define (Next-Floor up-or-down CF alod) (cond ((eq? 'up up-or-down) (Next-Floor-Up CF alod)) ((eq? 'down up-or-down) (Next-Floor-Down CF alod)) (else (error 'Next-Floor "bad input")))) ;; The elevator is going up. ;; Find out whether there are any demands for going up further. ;; If yes, pick the smallest number. That's the next floor. ;; If no, send the elevator down. The largest number is the next floor. (define (Next-Floor-Up CF alod) (cond ((cons? (demands-going-up alod CF)) (smallest (demands-going-up alod CF))) ((cons? alod) (largest alod)) (else CF))) ;; The elevator is going down. ;; Find out whether there are any demands for going down further. ;; If yes, pick the largest number. That's the next floor. ;; If no, send the elevator up. The largest number is the next floor. (define (Next-Floor-Down CF alod) (cond ((cons? (demands-going-down alod CF)) (smallest (demands-going-down alod CF))) ((cons? alod) (largest alod)) (else CF)))