{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

f-1 sol

# f-1 sol - CS 61A 1 Solutions to sample final exam#1 Higher...

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

CS 61A Solutions to sample final exam #1 1. Higher order function Given the hint, most people found this easy: (define (ordinal num) (lambda (lst) (list-ref lst (- num 1)))) The (- num 1) comes from the fact that list-ref counts element numbers from 0, but ordinal counts element numbers from 1. We defined it that way so that the name THIRD would make sense for the result of (ordinal 3)! The most common mistake was to *invoke* the function created with lambda, like this: (define (ordinal num) ((lambda (lst) (list-ref lst (- num 1))) LST) ;; wrong!!! Another elegant solution, not using the hint, was this: (define (ordinal num) (if (= num 1) car (compose (ordinal (- num 1)) cdr))) I like this one because it's entirely a manipulation of functions; there is no explicit reference to the list argument of the functions. The problem statement should have said "positive integer" rather than "nonnegative integer" (left over from an earlier version); we didn't pay attention to any code checking for a zero argument, but just graded the part for nonzero arguments. Scoring: No points off for (+ NUM 1) or just NUM. No points off for getting the arguments to list-ref backwards, but. .. No credit for just NUM, the list-ref args backwards, and using variable names that didn't make it clear which argument you thought was which: (define (ordinal x) (lambda (y) (list-ref x y))) ;; wrong! No credit for other mistakes. 2. Recursive to iterative process Most people found this easy, too. As usual, we need an extra variable to maintain the partial result in recursive calls of the helper: (define (count-evens ints) (define (helper INTS RESULT) (cond ((NULL? INTS) RESULT) ((EVEN? (CAR INTS)) (HELPER (CDR INTS) (+ RESULT 1))) (ELSE (HELPER (CDR INTS) RESULT)))) (helper INTS 0)) There were two common mistakes: returning zero in the base case, and forgetting the recursive call for even numbers:

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

View Full Document
((even? (car ints)) (+ result 1)) ;; wrong! Scoring: one point, all or nothing. 3. Abstract data types We made this easy by using FOREST as the formal parameter for the helper procedure. That was a big hint! There are three abstract data types in this problem: * a TREE has selectors DATUM and CHILDREN * the datum of a Tree is a WORD OR SENTENCE, with selectors FIRST and BUTFIRST * the children of a Tree is a FOREST, which is to say, a sequence of Trees; sequences have selectors CAR and CDR. So the solution is (define (find-san-cities tree) (if (null? (CHILDREN tree)) (if (equal? (FIRST (DATUM tree)) 'San) (list (DATUM tree)) '()) (san-helper (CHILDREN tree)))) (define (san-helper forest) (if (null? forest) '() (append (find-san-cities (CAR forest)) (san-helper (CDR forest))))) Some people invented selectors for a forest ADT: (define first-tree car) (define rest-of-forest cdr) and then used them in san-helper. We accepted this solution, although I don't think it's necessary to invent special types for sequences of particular kinds. A few people asked "What if the datum is a word instead of a sentence?" but
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 14

f-1 sol - CS 61A 1 Solutions to sample final exam#1 Higher...

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

View Full Document
Ask a homework question - tutors are online