{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs61a_final_fa00

This preview shows pages 1–5. 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 (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 filling in the blanks below. (define (count-evens ints) (define (helper ) (cond )) (helper )) 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 first 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. Fill 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

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}