cs61a_final_fa00 - CS 61A Final December 20, 2000 Your name...

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

View Full Document Right Arrow Icon
CS 61A Final — December 20, 2000 Your name login cs61a– This exam is worth 30 points, or about 21% of your total course grade. It contains 15 questions. This booklet contains 14 numbered pages including the cover page. Put all answers on these pages, please; don’t hand in stray pieces of paper. This is an open book exam. When writing procedures, don’t put in error checks. Assume that you will be given arguments of the correct type. READ AND SIGN THIS: I certify that my answers to this exam are all my own work, and that I have not discussed the exam questions or answers with anyone prior to taking this exam. If I am taking this exam early, I certify that I shall not discuss the exam questions or answers with anyone until after the scheduled exam time. 1–2 / 2 3 / 2 4–5 / 4 6 / 3 7 / 2 8 / 3 9 / 2 10–11 / 3 12–13 / 2 14 / 3 15 / 4 total / 30 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Question 1 (1 point): Write a procedure ordinal that takes a nonnegative integer argument n . It should return a procedure that takes a list as argument and returns the n th element of the list (counting from one): > (define third (ordinal 3)) > (third ’(John Paul George Ringo)) George Hint: list-ref Question 2 (1 point): The following recursive procedure takes a list of integers and returns the number of elements that are even. (define (count-evens ints) (cond ((null? ints) 0) ((even? (car ints)) (+ 1 (count-evens (cdr ints)))) (else (count-evens (cdr ints))))) Rewrite this as an iterative procedure by Flling in the blanks below. (define (count-evens ints) (define (helper ) (cond )) (helper )) 2
Background image of page 2
Your name login cs61a– Question 3 (2 points): The following program is designed for use with the “world tree” discussed in lecture, in which the nodes represent geographical regions (contries, states, cities). The selectors for trees are datum and children . The datum at each node is a word or sentence. The program, which returns a list of all the cities whose Frst word is “San,” does not respect data abstraction: (define (find-san-cities tree) (if (null? (cdr tree)) ; Is this node a city? (if (equal? (car (car tree)) ’San) (list (car tree)) ’()) (san-helper (cdr tree)))) (define (san-helper forest) (if (null? forest) ’() (append (find-san-cities (car forest)) (san-helper (cdr forest))))) In the version below, each car and cdr has been replaced by a blank. ±ill in the blanks with the correct selectors, respecting all the relevant abstract data types: (define (find-san-cities tree) (if (null? ( tree)) (if (equal? ( ( tree)) ’San) (list ( tree)) ’()) (san-helper ( tree)))) (define (san-helper forest) (if (null? forest) ’() (append (find-san-cities ( forest)) (san-helper ( forest))))) 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Question 4 (2 points): Consider the following program: (define (cost tree) (cost-help tree 0)) (define (cost-help tree above) (let ((new (+ (datum tree) above))) (make-node new (map (lambda (child) (cost-help child new)) (children tree))))) If cost is called with the following tree as its argument, draw the tree that it returns. 3
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.

This note was uploaded on 09/02/2010 for the course COMPSCI N/A taught by Professor Various during the Summer '01 term at University of California, Berkeley.

Page1 / 14

cs61a_final_fa00 - CS 61A Final December 20, 2000 Your name...

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