;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname lec8) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; A list-of-num is either ;; - empty ;; - (cons num list-of-num) ;; fun-on-lon : list-of-num -> ... #; (define (fun-on-lon lon) (cond [(empty? lon) ...] [(cons? lon) ... (first lon) ... (fun-on-lon (rest lon)) ...])) ;; feed-fish : list-of-num -> list-of-num ;; Feed every fish in lon 1 lb of food (define (feed-fish lon) (cond [(empty? lon) empty] [(cons? lon) (cons (+ 1 (first lon)) (feed-fish (rest lon)))])) (check-expect (feed-fish empty) empty) (check-expect (feed-fish (cons 7 empty)) (cons 8 empty)) (check-expect (feed-fish (cons 7 (cons 10 empty))) (cons 8 (cons 11 empty))) ;; large-fish : list-of-num -> list-of-num ;; To drop every fish strictly smaller than 5 lbs ;; from lon (define (large-fish lon) (cond [(empty? lon) empty] [(cons? lon) (cond [(< (first lon) 5) (large-fish (rest lon))] [else (cons (first lon) (large-fish (rest lon)))])])) (check-expect (large-fish empty) empty) (check-expect (large-fish (cons 7 empty)) (cons 7 empty)) (check-expect (large-fish (cons 5 empty)) (cons 5 empty)) (check-expect (large-fish (cons 4 empty)) empty) (check-expect (large-fish (cons 8 (cons 4 (cons 5 empty)))) (cons 8 (cons 5 empty))) ;; A list-of-posn is either ;; - empty ;; - (cons posn list-of-posn) ;; A posn is ;; (make-posn num num) ;; fun-for-lop : list-of-posn -> .... #; (define (fun-for-lop lop) (cond [(empty? lop) ] [(cons? lop) ... (fun-for-posn (first lop)) ... (fun-for-lop (rest lop)) ....])) ;; fun-for-posn : posn -> ... #; (define (fun-for-posn p) ... (posn-x p) ... (posn-y p) ...) ;; flip-posns : list-of-posn -> list-of-posn ;; To flip each posn in lop over the diagonal (define (flip-posns lop) (cond [(empty? lop) empty] [(cons? lop) (cons (flip-posn (first lop)) (flip-posns (rest lop)))])) ;; flip-posn : posn -> posn (define (flip-posn p) (make-posn (posn-y p) (posn-x p))) (check-expect (flip-posn (make-posn 3 4)) (make-posn 4 3)) (check-expect (flip-posns empty) empty) (check-expect (flip-posns (cons (make-posn 3 4) empty)) (cons (make-posn 4 3) empty)) (check-expect (flip-posns (cons (make-posn 3 4) (cons (make-posn -1 9) empty))) (cons (make-posn 4 3) (cons (make-posn 9 -1) empty))) #| ;; all-passed? : list-of-grade -> bool (define (all-passed? log) (cond [(empty? log) true] [(cons? log) (and (passed? (first log)) (all-passed? (rest log)))])) (check-expect (all-passed? empty) true) ... [painful without data defns, etc.] ... |# ;; A list-of-lon is either ;; - empty ;; - (cons list-of-num list-of-lon) ;; A list-of-num is either ;; - empty ;; - (cons num list-of-num) ;; sums : list-of-lon -> num ;; To add up all the numbers in all lists ;(define (sums lolon) ; (cond ; [(empty? lolon) ...] ; [(cons? lolon) ; ... (sum (first lolon)) ; ... (sums (rest lolon)) ...])) (define (sums lolon) (cond [(empty? lolon) 0] [(cons? lolon) (+ (sum (first lolon)) (sums (rest lolon)))])) ;; sum : list-of-num -> num ;(define (sum lon) ; (cond ; [(empty? lon) ] ; [(cons? lon) ; .. (first lon) ... ; ... (sum (rest lon)) ... ]) (define (sum lon) (cond [(empty? lon) 0] [(cons? lon) (+ (first lon) (sum (rest lon)))])) (check-expect (sum empty) 0) (check-expect (sum (cons 7 empty)) 7) (check-expect (sums empty) 0) (check-expect (sums (cons empty empty)) 0) (check-expect (sums (cons (cons 15 empty) empty)) 15) (check-expect (sums (cons (cons 9 (cons 15 empty)) empty)) 24) (check-expect (sums (cons (cons 9 (cons 15 empty)) (cons (cons 1 empty) empty))) 25) ;; Same test a above, using list shorthand for inner lists: (check-expect (sums (cons (list 9 15) (cons (list 1) empty))) 25) ;; Same test, using list shorthand for all lists: (check-expect (sums (list (list 9 15) (list 1))) 25)