;; ======================================== ;; list-of-num examples ;; ======================================== ; A list-of-num is ; - empty ; - (cons num list-of-num) ;(define (func-for-lon l) ; (cond ; [(empty? l) ...] ; [(cons? l) ; ... (first l) ; ... (func-for-lon (rest l)) ...])) ; feed-fish : list-of-num -> list-of-num ; Feeds each fish 1 lb (define (feed-fish l) (cond [(empty? l) empty] [(cons? l) (cons (+ 1 (first l)) (feed-fish (rest l)))])) (feed-fish empty) "should be" empty (feed-fish (cons 7 (cons 9 empty))) "should be" (cons 8 (cons 10 empty)) ; large-fish : list-of-num -> list-of-num ; Removes all fish < 5 lb (define (large-fish l) (cond [(empty? l) empty] [(cons? l) (cond [(< (first l) 5) (large-fish (rest l))] [else (cons (first l) (large-fish (rest l)))])])) (large-fish empty) "should be" empty (large-fish (cons 5 empty)) "should be" (cons 5 empty) (large-fish (cons 5 (cons 4 empty))) "should be" (cons 5 empty) (large-fish (cons 3 (cons 8 empty))) "should be" (cons 8 empty) ;; ======================================== ;; list-of-posn examples ;; ======================================== ; A list-of-posn is ; - empty ; - (cons posn list-of-posn) ; A posn is ; (make-posn num num) ;(define (func-for-lop l) ; (cond ; [(empty? l) ...] ; [(cons? l) ; ... (func-for-posn (first l)) ; ... (func-for-lop (rest l)) ...])) ; (define (func-for-posn p) ; ... (posn-x p) ... (posn-y p) ...) ; flip-posns : list-of-posn -> list-of-posn ; Flips all X & Y parts (define (flip-posns l) (cond [(empty? l) empty] [(cons? l) (cons (flip-posn (first l)) (flip-posns (rest l)))])) ; flip-posn : posn -> posn (define (flip-posn p) (make-posn (posn-y p) (posn-x p))) (flip-posn (make-posn 3 4)) "should be" (make-posn 4 3) (flip-posns empty) "should be" empty (flip-posns (cons (make-posn 3 4) empty)) "should be" (cons (make-posn 4 3) empty) (flip-posns (cons (make-posn 3 4) (cons (make-posn 6 8) empty))) "should be" (cons (make-posn 4 3) (cons (make-posn 8 6) empty)) ;; ======================================== ;; list-of-grade examples ;; ======================================== ; A list-of-grade is ; - empty ; - (cons grade list-of-grade) ; A grade is either ; - num ; - empty ;(define (func-for-log l) ; (cond ; [(empty? l) ...] ; [(cons? l) ; ... (func-for-grade (first l)) ; ... (func-for-log (rest l)) ...])) ;(define (func-for-grade g) ; (cond ; [(number? g) ...] ; [(empty? g) ...])) ; all-passed? : list-of-grade -> bool ; Determines whether all grades pass (define (all-passed? l) (cond [(empty? l) true] [(cons? l) (and (passing-grade? (first l)) (all-passed? (rest l)))])) ; passing-grade? : grade -> bool (define (passing-grade? g) (cond [(number? g) (>= g 70)] [(empty? g) false])) (passing-grade? 80) "should be" true (passing-grade? 70) "should be" true (passing-grade? 50) "should be" false (passing-grade? empty) "should be" false (all-passed? empty) "should be" true (all-passed? (cons 70 empty)) "should be" true (all-passed? (cons empty empty)) "should be" false ;; ======================================== ;; list-of-lon examples ;; ======================================== ; A list-of-lon is ; - empty ; - (cons list-of-num list-of-lon) ; A list-of-num is ; - empty ; - (cons num list-of-num) ;(define (func-for-lolon l) ; (cond ; [(empty? l) ...] ; [(cons? l) ; ... (func-for-lon (first l)) ; ... (func-for-lolon (rest l)) ...])) ;(define (func-for-lon l) ; (cond ; [(empty? l) ...] ; [(cons? l) ; ... (first l) ; ... (func-for-lon (rest l)) ...])) ; sums: list-of-lon -> list-of-num (define (sums l) (cond [(empty? l) empty] [(cons? l) (cons (aq-weight (first l)) (sums (rest l)))])) ; aq-weight : list-of-num -> num (define (aq-weight l) (cond [(empty? l) 0] [(cons? l) (+ (first l) (aq-weight (rest l)))])) (aq-weight empty) "should be" 0 (aq-weight (cons 3 empty)) "should be" 3 (sums empty) "should be" empty (sums (cons (cons 7 empty) (cons (cons 2 (cons 3 empty)) empty))) "should be" (cons 7 (cons 5 empty)) ; total-sum : list-of-lon -> num (define (total-sum l) (aq-weight (sums l))) (total-sum empty) "should be" 0 (total-sum (cons (cons 7 empty) (cons (cons 2 (cons 3 empty)) empty))) "should be" 12