Computer Science 61A - Spring 2006 - Harvey - Midterm 1 Solutions

# Computer Science 61A - Spring 2006 - Harvey - Midterm 1 Solutions

• Notes
• 6

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

1 of 6 5/11/2006 12:10 AM CS 61A Fall 2005 Midterm #1 solutions Note: If your individual score is I points (out of 40) and your group score is G points (out of 12), your overall score will be computed as (max I (+ (* 0.91666667 I) (* 0.3333 G))) The theory behind this formula is that 20 of your 40 individual points are not matched by anything in the group exam, and for the 10 points that are matched, we want to weight the exams as 2/3 individual, 1/3 group. So that gives (+ (* 30/40 I) (* 2/3 (* 10/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? Scoring: One point each, all or nothing, except that we deducted at most one point for quotation marks before/around any number of otherwise correct answers. *You* type quotation marks in front of sentences you want Scheme to treat as data rather than as procedure calls, but THE QUOTATION MARKS ARE NOT PART OF THE VALUES, AND SCHEME DOESN'T PRINT THEM. 1) > (keep (lambda (x) (or (even? x) (< (count x) 3))) '(1 12 123)) (1 12) (EVEN? X) is true for 12; (< (COUNT X) 3) is true for 1 and 12. So the (OR ...) is true for 1 and 12. 2) > (se '(procedures are) (first 'class)) (procedures are c) (FIRST 'CLASS) is its first letter, C. 3) > (every (* x x) '(4 5 6)) ERROR (unbound symbol x) The answer (16 25 36) would be correct if the question were (every (LAMBDA (X) (* x x)) '(4 5 6)) but in the actual question, the first argument to EVERY isn't a function; it's an attempt to compute the square of some particular number called X, and there isn't any such variable. 4) > (every first (keep even? '(23 48 12 87 6))) (4 1 6) The (KEEP ...) part returns (48 12 6), and so (EVERY ...) returns the first digits of each of those numbers. It's okay that 6 has only one digit; that just means that its last is the same as its first. And it's okay that 1 isn't even; 12 is even, and the KEEP testing happens before we take FIRST of anything. 5) > (word (first '(wish you)) (bf '(were here))) ERROR (Invalid argument to WORD: (HERE).) (BF '(WERE HERE)) is the sentences (HERE), not the word HERE. That would be (LAST '(WERE HERE)). So this expression gives the WORD procedure a sentence as its second argument. 6) > (cond ('comfortably 'numb) (hey you) (else money)) numb The first COND clause is ('comfortably 'numb). Since anything other than #F is considered true in Scheme, the word COMFORTABLY is true, and so the expression 'NUMB is evaluated to provide the return value for the COND. The remaining clauses are not evaluated, so it doesn't matter than HEY is an unbound variable. 2. Applicative vs. Normal Order

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

2 of 6 5/11/2006 12:10 AM Applicative: 5. Normal: 2. Actual Scheme: 5.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern