;; 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-intermediate-lambda-reader.ss" "lang")((modname week9_w_full) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "htdp"))))) ;; food-chain : list-of-numbers -> list-of-numbers ; takes weights of fish, returns weights after each fish eats those in front of it (define (food-chain l) (cond [(empty? l) l] [else (cons (first l) (add-to-each (first l) (food-chain (rest l))))])) ;; food-chain-acc : list-of-numbers number -> list-of-numbers ; takes weights of fish, returns weights after each fish eats those in front of it (define (food-chain-acc l sofar) (cond [(empty? l) empty] [else (cons (+ sofar (first l)) (food-chain-acc (rest l) (+ sofar (first l))))])) ; add-to-each : number list-of-numbers -> list-of-numbers ; adds n to each element of list (define (add-to-each n l) (map (lambda (x) (+ x n)) l)) ;(check-expect (add-to-each 3 (list 1 2 3)) (list 4 5 6)) ;(check-expect (food-chain (list 1 2 3 4 5)) (list 1 3 6 10 15)) ;(check-expect (food-chain-acc (list 1 2 3 4 5) 0) (list 1 3 6 10 15)) ;; parity : list-of-boolean -> boolean ;; returns true if there are an even number of booleans, and false otherwise (define (parity l) (cond [(empty? l) true] [else (xor (first l) (parity (rest l)))])) ;; xor : boolean boolean -> boolean (define (xor b1 b2) (or (and b2 (not b1)) (and b1 (not b2)))) ;(check-expect (xor true true) false) ;(check-expect (xor true false) true) ;(check-expect (xor false true) true) ;(check-expect (xor false false) false) ; ;(check-expect (parity (list)) ; true) ;(check-expect (parity (list false false false)) ; true) ;(check-expect (parity (list false true false true false)) ; true) ;(check-expect (parity (list false true false false)) ; false) ;(check-expect (parity2 (list)) ; true) ;(check-expect (parity2 (list false false false)) ; true) ;(check-expect (parity2 (list false true false true false)) ; true) ;(check-expect (parity2 (list false true false false)) ; false) (define (parity2 l) (parity/acc l true)) (define (parity/acc l acc) (cond [(empty? l) acc] [else (parity/acc (rest l) (xor (first l) acc))]))