;; 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-advanced-reader.ss" "lang")((modname week9_f_full) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "image.ss" "teachpack" "2htdp"))))) ; accumulators ; food-chain : list-of-numbers -> list-of-numbers ; computes result if fish of given size eat all fish in ; front of them (define (food-chain l) (cond [(empty? l) l] [else (cons (first l) (add-to-each (first l) (food-chain (rest l))))]) ) (check-expect (food-chain (list 1 2 3 5 7)) (list 1 3 6 11 18)) (define (add-to-each n l) (map (lambda (x) (+ x n)) l)) ; food-chain-acc : list-of-numbers number -> list-of-numbers (define (food-chain-acc lon sofar) (cond [(empty? lon) empty] [else (cons (+ (first lon) sofar) (food-chain-acc (rest lon) (+ (first lon) sofar)))] )) (check-expect (food-chain-acc (list 2 3 5 7) 1) (list 3 6 11 18)) (check-expect (food-chain-acc (list 3 5 7) 3) (list 6 11 18)) (define (food-chain2 lon) (food-chain-acc lon 0)) (check-expect (food-chain2 (list 1 2 3 5 7)) (list 1 3 6 11 18)) ; rules of accumulators: ; 1) identify the need for an accumulator ; 2) Maintain the accumulator ; 3) Exploit the accumulator ; parity : list-of-boolean -> boolean ; returns true if there are an even number of true booleans, false otherwise (define (parity lob) (even? (length (filter (lambda (x) x) lob)) )) (check-expect (parity (list false false true)) false) (check-expect (parity (list false true false true)) true) ; parity2 : list-of-boolean -> boolean ; returns true if there are an even number of true booleans, false otherwise (define (parity2 lob) (foldr xor true lob) ) (check-expect (parity2 (list false false true)) false) (check-expect (parity2 (list false true false true)) true) ;(true list-with-true-parity) -> false ;(true list-with-false-parity) -> true ;(false list-with-true-parity) -> true ;(false list-with-false-parity) -> false (define (xor a b) (or (and b (not a)) (and a (not b)))) ; number-new-lows : list-of-numbers -> number ; returns number of new lows achieved in list, assuming numbers < 5000 (define (number-new-lows lon) (number-new-lows-acc lon 5000) ) (check-expect (number-new-lows (list 4 3 2 3 2 1)) 4) (check-expect (number-new-lows (list 3 4 6 9 10)) 1) ; 4 3 2 3 2 1 => 1 2 3 2 3 4 ; 5 + 4 + 3 + 2 + 1 = n*(n-1) / 2 ; number-new-lows-acc : list-of-numbers number -> number ; returns number of new lows in list lower than minsofar ; assuming numbers < 5000 (define (number-new-lows-acc lon minsofar) (cond [(empty? lon) 0] [else (+ (cond[(< (first lon) minsofar) 1] [else 0]) (number-new-lows-acc (rest lon) (min minsofar (first lon))))] )) (check-expect (number-new-lows-acc (list 3 2 1) 2) 1) ;(number-new-lows (list 2 3 2 1)) =>...=> (number-new-lows-acc (list 3 2 1) ; 2) ; a graph is: ; (make-graph list-of-X (X -> list-of-X) (X X -> boolean)) (define-struct graph (nodes neighbor same-node?)) ; route-exists : graph symbol symbol -> boolean ; returns whether any route exists from src to dest nodes in graph ; ....