;; 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 ; sofar = sum of fish in front (define (food-chain-acc l sofar) (cond[(empty? l) empty] [else (cons (+ (first l) sofar) (food-chain-acc (rest l) (+ (first l) sofar)))] )) ; 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 true booleans, and false otherwise ;; xor : boolean boolean -> boolean (define (parity l) (cond [(empty? l) true] [else (xor (first l) (parity (rest l)))])) (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)) ;parity/acc : list-of-booleans boolean -> boolean ; outputs parity assuming parity of list so far is acc (define (parity/acc l acc) (cond[(empty? l) acc] [else (parity/acc (rest l) (xor (first l) acc))] )) ; key fact: parity/acc is TAIL RECURSIVE ; fib-fast: number -> number ; returns the nth Fibonacci number (define (fib-fast n) (cond [(= n 0) 1] [(= n 1) 1] ; [else (fib-fast-acc (- n 2) 1 1)])) [else (local [(define fnminus2 (fib-fast (- n 2)))] (+ (fib-fast-with-prev (- n 1) fnminus2) fnminus2))])) ; fib-fast-with-prev : number number -> number ; returns the nth Fibonacci number given the previous one (define (fib-fast-with-prev n prev) (cond [(= n 0) 1] [(= n 1) 1] [else (+ (fib-fast (- n 2)) prev)])) ; fib-fast-acc : number number number -> number ; returns the nth higher Fibonacci number given the previous two numbers (define (fib-fast-acc n fib-1 fib-2) (cond [(= n 0) (+ fib-1 fib-2)] [else (fib-fast-acc (- n 1) (+ fib-1 fib-2) fib-1)]))