cs61a_sp08_mt1_sol

# Cs61a_sp08_mt1_sol - CS61A Spring 2008 Midterm 1 solutions Note If your individual score is I points(out of 40 and your group score is G points(out

This preview shows pages 1–3. Sign up to view the full content.

CS61A Spring 2008 Midterm 1 solutions Note: If your individual score is I points (out of 40) and your group score is G points (out of 11), your overall score will be computed as (max I (+ (* 0.9083333 I) (* 0.3333 G))) The theory behind this formula is that 29 of your 40 individual points are not matched by anything in the group exam, and for the 11 points that are matched, we want to weight the exams as 2/3 individual, 1/3 group. So that gives (+ (* 29/40 I) (* 2/3 (* 11/40 I)) (* 1/3 G)), except that if your individual score is better than the combined score by this formula, we'll keep your individual score. But this correction is done at the end of the semester in the final grade-reporting program; GLOOKUP isn't smart enough to report anything but a fixed weighting of points. 1. What will Scheme print? (every (lambda (x) (/ x 2)) (keep even? (every (lambda (x) (* x x)) '(2 3 4 5)))) Answer: (2 8) The inner EVERY computes the square of each number, giving (4 9 16 25). The KEEP keeps the even ones, giving (4 16). Then the outer EVERY divides each of those numbers by 2. (map (lambda (x) (se x x)) '(a b c)) Answer: ((a a) (b b) (c c)) MAP computes the given function for each element of its data list (A B C), and returns a list in which each element is the result of one of those function calls. For a three-element argument list, MAP always returns a three-element result list. By the way, this isn't a data abstraction violation. Yes, since we're using MAP, we're thinking of (A B C) as a list, not as a sentence, but its elements are still words, so it's okay to call SENTENCE on them. The result is a /list of sentences/. (every (lambda (x) (se x x)) '(a b c)) Answer: (A A B B C C) The difference between this problem and the one before is that the overall result is put together using SENTENCE as the combiner, instead of using CONS to make a list. SENTENCE flattens the result, because it appends the sentences (A A), (B B), and (C C) into one big sentence.

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

View Full Document
(map (lambda (x) (let ((x (+ x 1)) (y x)) (* x y))) '(2 5)) Answer: (6 30) Of course nobody would really write an expression like this in which the name X is used for two different variables. But we wanted to see if you understand how LET works. So, the MAP calls the LAMBDA procedure twice, once with X=2 and once with X=5. Here's what happens when I substitute 2 for X in all the places that use the X from the LAMBDA expression, rather than the X from the LET: (let ((x (+ 2 1)) (y 2)) (* x y)) So, inside the LET body, X=3 but Y=2. The key point is that the expressions that provide values for the LET binding values are all evaluated /before/ the let bindings happen. In particular, the X that provides a value for Y is the LAMBDA's X, not the LET's X. Similarly, for the X=5 case, the LET variables have the value X=6,
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/23/2010 for the course CS 61A taught by Professor Harvey during the Fall '08 term at University of California, Berkeley.

### Page1 / 10

Cs61a_sp08_mt1_sol - CS61A Spring 2008 Midterm 1 solutions Note If your individual score is I points(out of 40 and your group score is G points(out

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

View Full Document
Ask a homework question - tutors are online