;; ===================================================================================== ;; _____ __ __ ____ _ __ __ ____ ___ ;; / ____/ |/ / __ \/ / / / < / __ < / Spring 2019 Section 54 ;; / / / /|_/ / /_/ / / / /_____/ / / / / / ;; / /___/ / / / ____/ /_/ /_____/ / /_/ / / Assignment 04 ;; \____/_/ /_/_/ \____/ /_/\____/_/ ;; ;; S .d8888. .d88b. db db db d888888b d888888b .d88b. d8b db .d8888. ;; A 88' YP .8P Y8. 88 88 88 `~~88~~' `88' .8P Y8. 888o 88 88' YP ;; M `8bo. 88 88 88 88 88 88 88 88 88 88V8o 88 `8bo. ;; P `Y8b. 88 88 88 88 88 88 88 88 88 88 V8o88 `Y8b. ;; L db 8D `8b d8' 88booo. 88b d88 88 .88. `8b d8' 88 V888 db 8D ;; E `8888Y' `Y88P' Y88888P ~Y8888P' YP Y888888P `Y88P' VP V8P `8888Y' ;; ;; ===================================================================================== (load "asmt-helper.txt") (header "Professor Ellis" "Sample Solutions to Assignment Four") (problem "1 (A)") ;; ============================================================================= ;; BOOKSTORE-OPEN? ;; ============================================================================= ;; Determines if the CMPU-101 Bookstore is open. ;; ----------------------------------------------------------------------------- ;; INPUTS: DAY, a symbol, one of SUN, MON, TUE, ..., FRI, SAT ;; HOUR, an integer from 1 to 12, inclusive ;; MINUTES, an integer from 0 to 59, inclusive ;; AM-OR-PM, a symbol, either AM or PM ;; OUTPUT: #t, if the inputs specify one of the following: ;; Saturday from 11:45 am to 12:15 pm, inclusive. ;; All day Tuesday, except from 12:01 pm to 12:59 pm, inclusive; ;; #f otherwise. ;; NOTE: This version uses COND and IF. ;; ============================================================================= (define bookstore-open? (lambda (day hour minutes am-or-pm) (cond ((eq? day 'tue) (if (eq? am-or-pm 'pm) (if (= hour 12) (if (> minutes 0) #f #t) #t) #t)) ((eq? day 'sat) (if (eq? am-or-pm 'am) (if (= hour 11) (if (>= minutes 45) (if (<= minutes 59) #t #f) #f) #f) (if (= hour 12) (if (< minutes 16) #t #f) #f))) (else #f)))) (tester '(bookstore-open? 'sat 11 30 'am)) (tester '(bookstore-open? 'sat 11 50 'am)) (tester '(bookstore-open? 'sat 12 12 'pm)) (tester '(bookstore-open? 'sat 12 49 'pm)) (problem "1 (B)") ;; ============================================================================= ;; BOOKSTORE-OPEN? ;; ============================================================================= ;; Determines if the CMPU-101 Bookstore is open. ;; ----------------------------------------------------------------------------- ;; INPUTS: DAY, a symbol, one of SUN, MON, TUE, ..., FRI, SAT ;; HOUR, an integer from 1 to 12, inclusive ;; MINUTES, an integer from 0 to 59, inclusive ;; AM-OR-PM, a symbol, either AM or PM ;; OUTPUT: #t, if the inputs specify one of the following: ;; Saturday from 11:45 am to 12:15 pm, inclusive. ;; All day Tuesday, except from 12:01 pm to 12:59 pm, inclusive; ;; #f otherwise. ;; NOTE: This version *does not use* COND and IF. ;; ============================================================================= (define bookstore-open?-v2 (lambda (day hour minutes am-or-pm) (or (and (eq? day 'tue) (not (and (eq? am-or-pm 'pm) (= hour 12) (> minutes 0) (<= minutes 59))))) (and (eq? day 'sat) (or (and (eq? am-or-pm 'am) (= hour 11) (>= minutes 45) (<= minutes 59)) (and (eq? am-or-pm 'pm) (= hour 12) (< minutes 16)))))) (tester '(bookstore-open? 'sat 11 30 'am)) (tester '(bookstore-open? 'sat 11 50 'am)) (tester '(bookstore-open? 'sat 12 12 'pm)) (tester '(bookstore-open? 'sat 12 49 'pm)) (problem "2") ;; ============================================================================= ;; OFFICE-OPEN?-ALT ;; ============================================================================= ;; Determines if the office is open. ;; ----------------------------------------------------------------------------- ;; INPUTS: DAY, a symbol, one of SUN, MON, TUE, ..., FRI, SAT ;; AM-OR-PM, a symbol, either AM or PM ;; OUTPUT: #f, if Friday any time, or Tuesday morning. ;; #t otherwise. ;; NOTE: This version *does not use* COND and IF. ;; ============================================================================= (define office-open?-alt (lambda (day am-or-pm) (not (or (eq? day 'fri) (and (eq? am-or-pm 'am) (eq? day 'tue)))))) (tester '(office-open?-alt 'fri 'am)) (tester '(office-open?-alt 'thu 'pm)) (tester '(office-open?-alt 'wed 'am)) (tester '(office-open?-alt 'wed 'pm)) (tester '(office-open?-alt 'mon 'am)) (tester '(office-open?-alt 'tue 'am)) (tester '(office-open?-alt 'tue 'pm)) (problem "3") ;; ============================================================================= ;; SUM-RECIPS ;; ============================================================================= ;; Adds the sum of reciprocals from 1..N. ;; ----------------------------------------------------------------------------- ;; INPUTS: N, a positive integer. ;; OUTPUT: The sum 1/1 + 1/2 + 1/3 + ... + 1/N. ;; ============================================================================= (define sum-recips (lambda (n) (if (= n 1) 1 (+ (/ 1 n) (sum-recips (- n 1)))))) (tester '(sum-recips 3)) (tester '(sum-recips 2)) (tester '(sum-recips 10)) (tester '(sum-recips 1)) (tester '(sum-recips 25)) (problem "4") ;; ============================================================================= ;; ALT-SUM ;; ============================================================================= ;; Computes the sum of alternating - and + reciprocals 1/N; - for even N and ;; + for odd N. ;; ----------------------------------------------------------------------------- ;; INPUTS: N, a positive integer. ;; OUTPUT: The alternating sum of reciprocals in N, such that ;; ;; 1 1 1 1 1 ;; 1 - - + - - - + - - ... ∓ - ;; 2 3 4 5 N ;; ;; NOTE: Output should be in decimal ('floating point') form, not a fraction. ;; ============================================================================= (define alt-sum (lambda (n) (cond ((< n 2) (* 1.0 n)) ((even? n) (+ (/ -1.0 n) (alt-sum (- n 1)))) (else (+ (/ 1.0 n) (alt-sum (- n 1))))))) (tester '(alt-sum 1)) (tester '(alt-sum 3)) (tester '(alt-sum 5))