cs61a_sp09_f_sol - CS 61A 1. Spring, 2009 Final exam...

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

View Full Document Right Arrow Icon
CS 61A Spring, 2009 Final exam solutions 1. Applicative/normal order. (define (if-false x y) (if x 'true y)) (if-false (/ 33 0) (/ 33 1)) In APPLICATIVE order, both of the actual argument expressions are evaluated before the function is called, so the expression (/ 33 0) is evaluated and gives an ERROR. In NORMAL order, the argument expressions are not evaluated, but are substituted into the body: (if (/ 33 0) 'true (/ 33 1)) The IF expression has to evaluate its first argument in order to know which of the other two it should choose. So (/ 33 0) is still evaluated, and we still get an ERROR, although it comes a little later in the process. (if-false (/ 33 1) (/ 33 0)) In APPLICATIVE order, both argument expressions are evaluated right away, as in the first example, so we again get an ERROR. In NORMAL order, the argument expressions are substituted into the body: (if (/ 33 1) 'true (/ 33 0)) The first argument to IF is evaluated; its value is 33, which is considered true in Scheme, so IF evaluates its second argument and returns the word TRUE. (No quotation mark!) The expression (/ 33 0) is never evaluated, so there is no error. Scoring: One point per answer. 2. Higher order functions. In order for this to work even with the possibility of negative numbers, you can't use a fixed base case for the accumulation with MAX. Instead you
Background image of page 1

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

View Full DocumentRight Arrow Icon
have to find the first number in the list, if any, and use that as the base case for the accumulation: (define (maxnum lst) (let ((nums (filter number? lst))) (if (null? nums) #f (accumulate max (car nums) (cdr nums))))) The four-point positive-number-only solution would be this: (define (maxnum lst) ; not perfect but at least elegant :-) (accumulate max 0 (filter number? lst))) Another correct solution avoids the problem of the base case for ACCUMULATE by using APPLY instead. This works only if you first ensure that there is at least one number; you can't apply MAX to no arguments: (define (maxnum lst) (let ((nums (filter number? lst))) (if (null? nums) #f (apply max nums)))) Why do we need APPLY here? Why not just (MAX NUMS)? The MAX primitive does not take a list as argument. It takes one or more /numbers/ as argument(s). Many people misunderstood what the problem is about negative numbers. It's not that negative numbers are excluded from the domain of MAX! People who took pains to reinvent their own MAX didn't benefit from doing so (although it didn't hurt either). The problem is mathematical; there is no identity element for the MAX function, unlike + (identity 0) and * (identity 1). This is still true even if you write it yourself. Similarly, explicitly looking for a minus sign as the FIRST of a number is unnecessary and unhelpful. Because the argument is a list that might include sublists, and not a
Background image of page 2
Image of page 3
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 / 22

cs61a_sp09_f_sol - CS 61A 1. Spring, 2009 Final exam...

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

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