cs61a_sp09_f

# cs61a_sp09_f - CS 61A Final Exam Your name login cs61a This...

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

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

View Full Document
Question 1 (4 points): (define (if-false x y) (if x ’true y)) For each of the following expressions, state the result in applicative order and in normal order. If the expression results in an error, just say ERROR; you don’t have to give the exact message. (if-false (/ 33 0) (/ 33 1)) Applicative: Normal: (if-false (/ 33 1) (/ 33 0)) Applicative: Normal: Question 2 (5 points): De±ne a function maxnum that takes a list as argument. The elements of the list might or might not include numbers, along with other values. If any elements are numbers, return the largest number. If not, return #f . If an element is a list, don’t look inside it; consider only top-level elements. > (maxnum ’(20 minutes and 45 seconds after 10 (2009 5 15))) 45 Use higher-order functions, not recursion! Note: You get 4 points if your function works only for positive numbers. 2
Your name login cs61a– Question 3 (4 points): Here are two procedures that use two diferent algorithms to solve the same problem: Given two lists oF length n as arguments, return true iF they have any elements in common, False iF not. ±or each procedure, indicate the order oF growth in time, and whether the procedure generates an iterative or a recursive process. (define (common? ls1 ls2) (cond ((null? ls1) #f) ((null? ls2) #f) ((equal? (car ls1) (car ls2)) #t) (else (or (common? ls1 (cdr ls2)) (common? (cdr ls1) ls2))))) Θ(1) Θ( n ) Θ( n 2 ) Θ(2 n ) Iterative Recursive (define (common? ls1 ls2) (cond ((null? ls1) #f) ((member (car ls1) ls2) #t) (else (common? (cdr ls1) ls2)))) Θ(1) Θ( n ) Θ( n 2 ) Θ(2 n ) Iterative Recursive Question 4 (2 points): (define my-stream (cons-stream 3 (cons-stream 4 (add-streams my-stream (stream-cdr my-stream))))) > (show-stream my-stream 5) 3

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}