;; ;; Answers to CS3 2001 Summer Final Exam (by Lisa Nguyen) ;; ;; Question 1 ;; Part a ;; (cons cons cons) ;; ==> (#[procedure cons] . #[procedure cons]) ;; Part b ;; _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ;; | | | | | | | | | | | / | ;;-->| | - - ->| | - - ->| | - - ->| | / | ;; |_ | _ |_ _ _| |_ | _ |_ _ _ | |_ | _ |_ _ _ | |_ | _ |/ _ _ | ;; | | | | ;; | \/ \/ \/ ;; \/ 3 4 5 ;; _ _ _ _ _ _ _ _ _ _ _ _ ;; | | | | | / | ;; | | - - ->| | / | ;; |_ | _ |_ _ _ | |_ | _ |/ _ _ | ;; | | ;; \/ \/ ;; 1 2 ;; Part c ;; Name: SHOW-ME-THE-TRUTH ;; Inputs: A single argument of any data type, bool ;; Requires: None ;; Side Effects: Displays "false" and newline if bool is #f, and ;; displays "true" and a newline otherwise. ;; Returns: #f ;; Examples: (show-me-the-truth #t) ==> #f ;; ==P [true ;; ] ;; Part d ;; a ;; / \ ;; b c ;; /\ ;; d e ;; Part e ;; MYSTERY will return an error if given a tree with only one child. ;; Part f ;; Q: If I type (mystery thistree), what return value do I get? ;; A: this-is-hard ;; Q: Write all of the side effects of (mystery thistree)? ;; A: b ;; d ;; e ;; c ;; a ;; Question 2 ;; Part a we're skipping because we didn't read this chapter this year. ;; Part b ;; month-name, date-in-month ;; Part c ;; ABSTRACTION - You only needed to change two functions in datesv1.scm because ;; you use selectors. Because the rest of the code uses the selectors, it ;; doesn't really matter how the dates are actually implemented. If we hadn't ;; used selectors in the rest of the code, and just assumed that the month ;; would be car and the date would be cadr, you would have to change ALL the ;; functions in datesv1.scm. ;; Question 3 (define (cooler-deeper-count ltr s) (accumulate + (every (lambda (wd) (count (keep (lambda (l) (equal? ltr l)) wd))) s))) ;; Question 4 (define (make-term coeff expt) (cond ((= coeff 0) 0) ((= expt 0) coeff) ((and (= coeff 1) (= expt 1)) 'x) ((= coeff 1) (word 'x^ expt)) ((= expt 1) (word coeff 'x)) (else (word coeff 'x^ expt)))) ;; requires wd be in s ;; counts from 0 (define (position wd s) (if (equal? wd (first s)) 0 (+ 1 (position wd (bf s))))) (define (get-coefficient exp) (cond ((equal? (first exp) 'x) 1) ((not (member? 'x exp)) exp) (else ((repeated bl (- (count exp) (position 'x exp))) exp)))) (define (get-exponent exp) (cond ((number? exp) 0) ((not (member? '^ exp)) 1) (else ((repeated bf (+ 1 (position '^ exp))) exp)))) (define (deriv-term exp) (make-term (* (get-coefficient exp) (get-exponent exp)) (- (get-exponent exp) 1)))