CS61A_sp98_mt2_sol

CS61A_sp98_mt2_sol - Solutions for Midterm 1 CS 61A Spring...

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

View Full Document Right Arrow Icon
;; Solutions for Midterm 1 ;; CS 61A: Spring 1998 ;; ----------------------------------- ;; QUESTION 1 (6 points): all versions ;; (in order) (define x1 (list (cons 'a 'b) (list 'c 'd))) ;; ((a . b) (c d)) ;; +-+-+ +-+-+ ;; x1-->|*|*+--->|*|/| ;; +++-+ +++-+ ;; | | ;; | | ;; V V ;; +-+-+ +-+-+ +-+-+ ;; |*|*| |*|*+-->|*|/| ;; +++++ +++-+ +++-+ ;; | | | | ;; V V V V ;; a b c d (define x2 (cons 'a (cons 'b (cons 'c 'd)))) ;; (a b c . d) ;; +-+-+ +-+-+ +-+-+ ;; x2-->|*|*|-->|*|*|-->|*|*| ;; +++-+ +++-+ +++++ ;; | | | | ;; V V V V ;; a b c d (define x3 (list 'a 'b 'c (cons 'd '()))) ;; (a b c (d)) ;; +-+-+ +-+-+ +-+-+ +-+-+ ;; x3-->|*|*|-->|*|*|-->|*|*|-->|*|/| ;; +++-+ +++-+ +++-+ +++-+ ;; | | | | ;; V V V V ;; a b c +-+-+ ;; |*|/| ;; +++-+ ;; | ;; V ;; d (define x4 (append (list (cons 'a 'b)) (list '()))) ;; ((a . b) ()) ;; .-.-. .-.-. ;; x4 -> |*|*+-->|/|/| ;; `+"-' `-"-' ;; |
Background image of page 1

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

View Full DocumentRight Arrow Icon
;; .-.-. ;; |*|*| ;; `+"+' ;; | | ;; a b (define x5 '((1 2) 3 . 4)) ;; ((1 2) 3 . 4) ;; .-.-. .-.-. ;; x5 -> |*|*+-->|*|*| ;; `+"-' `+"+' ;; | | | ;; | 3 4 ;; | ;; .-.-. .-.-. ;; |*|*+-->|*|/| ;; `+"-' `+"-' ;; | | ;; 1 2 (define x6 '(1 (2 . 3))) ;; (1 (2 . 3)) ;; .-.-. .-.-. ;; x6 -> |*|*+-->|*|/| ;; `+"-' `+"-' ;; | | ;; 1 .-.-. ;; |*|*| ;; `+"+' ;; | | ;; 2 3 ;; -1 for each ;; -2 max for missing leading arrows ;; NOTE: Anything with a quote in it is considered to be unresolved and ;; credit was taken off for it. Missing pointers were taken off ;; partial credit because we couldn't tell if you understood what ;; was being pointed to. ;; ---------------------------------- ;; QUESTION 2 (6 points): all verions (define (chop m) (map cdr (cdr m))) (define (foo m) (if (empty? m) '() (cons m (foo (chop m))))) (define (accumulate op initial sequence) (if (null? sequence) initial
Background image of page 2
(op (car sequence) (accumulate op initial (cdr sequence))))) ;; Version A: (define H '((1 2 3) (4 5 6) (7 8 9))) ;; A. (chop H) ;; ((5 6) (8 9)) ;; B. (foo H) ;; (((1 2 3) (4 5 6) (7 8 9)) ((5 6) (8 9)) ((9))) ;; C. (accumulate * 2 (map caar (foo H))) ;; 90 ;; Version X: (define H '((1 3 5) (7 9 11) (13 15 17))) ;; A. (chop H) ;; ((9 11) (15 17)) ;; B. (foo H) ;; (((1 3 5) (7 9 11) (13 15 17)) ((9 11) (15 17)) ((17))) ;; C. (accumulate + 2 (map caar (foo H))) ;; 29 ;; GRADING: 2 points each ;; parital credit only if your error was ;; small ;; -------------------------------- ;; QUESTION 3 (5 points): version A (define (triad left label right) (lambda (m) (cond ((eq? m 't-car) left) ((eq? m 't-cbr) label) ((eq? m 't-cdr) right)))) ;; A lot of people got this question wrong. Remember that ;; this question is about message passing! Here are the ;; correct answers: (define (label node) (NODE 'T-CAR)) (define (left node) (NODE 'T-CBR)) (define (right node) (NODE 'T-CDR))
Background image of page 3

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

View Full DocumentRight Arrow Icon
;; node is a triad! As such, it should be able to take ;; 't-car, 't-cbr, or 't-cdr as its argument, and return ;; the appropriate value. ;; 3 points for this part of the question.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

CS61A_sp98_mt2_sol - Solutions for Midterm 1 CS 61A Spring...

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

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