Lesson Plan - 6.001 SP04 - recitation 6 lists and hops let desugar writing list procs (define (make-units C L H) (list C L H)) (define get-units-C car) (define get-units-L cadr) (define get-units-H caddr) (define (make-class number units) (list number units)) (define get-class-number car) (define get-class-units cadr) (define (get-class-total-units class) (+ (get-units-C (get-class-units class)) (get-units-L (get-class-units class)) (get-units-H (get-class-units class)))) (define (same-class? c1 c2) (= (get-class-number c1) (get-class-number c2))) note - ask not to use hops (define (total-scheduled-units sched) (if (null? sched) 0 (+ (get-class-total-units (car sched)) (total-scheduled-units (cdr sched))))) (define (drop-class sched classnum) (cond ((null? sched) nil) ((= (get-class-number (car sched)) classnum) (cdr sched)) (else (cons (car sched) (drop-class sched classnum))))) (define (credit-limit sched max-credits) (if (> (total-scheduled-units sched) max-credits) (credit-limit (cdr sched) max-credits) sched)) HOPs - using procedures like normal data (make-student 3495873459 (lambda (sched) (not (null? sched)))) (make-student 3495873459 (lambda (sched) (< (total-scheduled-units sched) 54))) (define (class-numbers sched) (map get-class-number sched)) (define (drop-class sched classnum) (filter (lambda (class) (not (= (get-class-number class) classnum))) sched)) (define (total-scheduled-units sched) (fold-right 0 + (map get-class-total-units sched))) (define (credit-limit sched limit) (fold-right (empty-schedule) (lambda (class sched) (if (< (total-scheduled-units (add-class class sched)) limit) (add-class class sched) sched)) sched)) slick solution: (O(n), not O(n^2)) (define (credit-limit sched limit) (car (fold-right (list (empty-schedule) 0) (lambda (class sched) (if (< (+ (get-class-total-units class) (cadr sched)) limit) (list (add-class class (car sched)) (+ (get-class-total-units class) (cadr sched))) sched)) sched)))