CS61A_sp98_mt1_sol - ; Solutions for Midterm 1 ; CS 61A:...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
;; Solutions for Midterm 1 ;; CS 61A: Spring 1998 ;; ----------------------- ;; (in order) ;; Q1: ;; error ;; 20 ;; (hip1 hip2 hip3 hurrah) ;; 21 ;; error ;; error ;; 5 ;; Q2: ;; F ;; F ;; T ;; F ;; F ;; ----------------------- ;; QUESTION 1 & 2 versions XYZ ;; 1a. (1 point) ;; ERROR: Invalid argument to WORD: (+ 2 3) ;; 1b. (1 point) ;; 20 ;; 1c. (1 point) (I was case insensitive.) ;; (HIP1 HIP2 HIP3 HURRAH) ;; 1d. (1 point) ;; 21 ;; 1e. (1 point) ;; ERROR: unbound variable: a ;; 1f. (1 point) ;; 25 ;; 1g. (1 point) ;; 6 ;; ---- ;; 2a. (1 point) ;; False ;; 2b. (1 point) ;; False ;; 2c. (1 point) ;; False ;; 2d. (1 point) ;; False
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
;; 2e. (1 point) ;; True ;; ---------------------------------- ;; QUESTION 3 versions ABC 5 points ;; There were basically 2 ways to do this problem: one good, ;; one not-so-good. The best, most straight-forward way of ;; doing this problem was in the following recursive manner: (define (subsequence f sen) (if (empty? sen) '() (se (f sen) (subsequence f (bf sen))))) ;; But because of the unique nature of the sentence function, ;; this iterative solution was also OK: (define (subsequence f sen) (define (iter sen result) (if (empty? sen) result (iter (bf sen) (se result (f sen))))) (iter sen '())) ;; This takes advantage of se's unusual property that ;; (se '() 5) gives (5), and (se '(5 6) 7) gives ;; (5 6 7), etc. Once you get used to cons, car, cdr, etc., ;; you'll notice that they won't do this like se does. ;; Most other correct answers were merely convoluted variations ;; on the above. ;; Some common errors: ;; Using (apply f sen) instead of (f sen): -2 ;; Wrong base case (it's not zero!!): -1 ;; Poor syntax, grossly incorrect parens, extra lambda: -1 ;; No recursive call, but hints at it: -2 ;; No recursive call, and has no clue: -5 ;; Incorrect args passed to subsequence: -2 ;; Incorrect algorithm, with shades of correctness: -2 ;; Incorrect algorithm, with no clue: -5 ;; ---------------------------------- ;; QUESTION 3 versions XYZ 5 points ;; Count down to zero version. (define (li-nth n lst) (cond ((empty? lst) '()) ((= n 0) (first lst)) (else (li-nth (- n 1) (bf lst))))) ;; Count up to n version. (define (li-nth n lst) (define (li-nth-helper count lst) (cond ((empty? lst) '())
Background image of page 2
((= count n) (first lst)) (else (li-nth-helper (+ count 1) (bf lst))))) (li-nth-helper 0 lst)) ;; -1 if your base cases were out of order ;; -2 missing base case ;; -3 made the solutions a linearly recursive process rather
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

CS61A_sp98_mt1_sol - ; Solutions for Midterm 1 ; CS 61A:...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online