





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A part of the cs 61a course materials from the university of california, berkeley, focusing on scheme programming. It explains the concept of map function in scheme using a list (1 2 3) as an example, discussing the box and pointer diagram for a pair that isn't a list, and addressing common abstraction violations. Additionally, it covers the concept of scheme-1 and the number of calls to eval-1 with each kind of expression.
Typology: Exams
1 / 9
This page cannot be seen from the preview
Don't miss anything!






CS 61A Spring 2008 Midterm 2 solutions
(map list '(1 2 3)) ((1) (2) (3)) ---->XX------>XX------>X/ | | | | | | V V V X/ X/ X/ | | | V V V 1 2 3 MAP calls LIST three times, once for each number in the argument list: (LIST 1), (LIST 2), and (LIST 3). The values returned by these LIST calls make up the elements of the returned list. (let ((x '(1 2)) (y '(8 9))) (cons x (append y x))) ((1 2) 8 9 1 2) +-------------+ | | --->** | x | | | V | V XX------>XX--+--->XX------>X/ | | | | V V V V 8 9 1 2 ^ ^ | | y-->XX------>X/ This picture shows why start arrows are important! The first pair of the result is the one shown as ** (instead of XX like the other pairs). The CAR of that pair is the value of X, which is the list (1 2) formed by the two rightmost pairs of the diagram. The ** pair is the one pair formed by the call to CONS. The call to APPEND produces the four-element list whose spine is the row of four pairs below the ** pair. The first two pairs are made by APPEND; the last two pairs are the ones shared with the value of X. (Note that the two pairs forming the value of Y are /not/ part of the returned value, so it's okay if you left them out of your diagram.) (cons (cons 1 2) (append '(18 3) '()))
The main point here was to see if you could draw a correct box and pointer diagram for a pair that isn't a list, the one that prints as (1. 2). (By the way, is this result a list? Should LIST? of it return true? Yes, because the definition of a list only mentions the CDRs of the spine pairs: a list is either the empty list or /a pair whose CDR is a list/. That's true of this structure, even though the /CAR/ of one of the pairs isn't a list. Some people thought that (1. 2) was a 3-element list with 1, a period, and 2. The dot in "dotted pair" notation is part of the notation, just as the parentheses are part of the notation, not part of the actual data. Scoring: One point per printed form, one point per diagram. At most one point lost for quotation marks anywhere in the answer.
Scoring: 1/2 point off per error (either failing to change something that should be changed, or changing something that shouldn't). This includes errors in places other than the ten we identified as possibilities, i.e., changing the name of CAR-FN to DATUM-FN counts as an error. The total is rounded down, but with a floor of zero.
2 numbers 2 special forms 0 applications of primitive procedures 1 application of a non-primitive procedure. Some students misunderstood the problem as asking about the different expressions evaluated /by STk/ during the invocation of EVAL-1, i.e., the number of numbers, etc., within the body of EVAL-1 itself. But the problem statement was unambiguous; it doesn't allow for that interpretation. Scoring: 1/2 point each, rounded down.
(same-structure? (branch-structure branch1) (branch-length branch1)) would be really confused. Many students tried to combine SAME-STRUCTURE? and SAME-BRANCH? into a single procedure. This is theoretically possible (just substitute the body of SAME-BRANCH? in place of each invocation of SAME-BRANCH? inside SAME-STRUCURE?), but the result is long and ugly, and it's very easy to get wrong. One common mistake was to check if the lengths match (maybe rotated), and check if the structures match (ditto), but allow the lengths to be rotated while the structures aren't or vice versa. For example, a (balanced) mobile whose left branch is a 3-gram weight at length 4 cm, and whose right branch is a 2-gram weight at length 6 cm was incorrectly said to match an unbalanced mobile whose left branch is a 2-gram weight at length 4 cm, and whose right branch is a 3-gram weight at length 6 cm. Another mistake that some people made by tring to cram everything into one procedure was to check that the left branch of struct1 matches either branch of struct2, and to check that the right branch of struct1 matches either branch of struct2. This incorrectly returns #T if struct1 is symmetric (both branches the same), but struct2 isn't. An even worse confusion was to treat the mobile as if it were a non-binary tree, with a forest full of children. Invocations of CAR and CDR were a likely indicator of this sort of problem; invocations of DATUM and CHILDREN definitely showed this confusion. Scoring: 8: perfect. 6: Doesn't check for length and structure simultaneously, doesn't check if one is mobile and one is not. 5: Missing weight base case. 4: Mix up mobiles and weights. 2: Just total weight compare; uses list procedures.
to write THREE-HUNDRED-BRANCHING? we wouldn't have wanted to do it that way. Those three tests can be replaced by a use of higher order functions: (and (= (length lst) 3) (= (length (filter three-branching? lst) 3)) (As always, when you are asking a yes-or-no question about a list it's much cleaner to use FILTER than to use MAP and then have a second pass going through the resulting list of #T and #F values.) Alternatively, you can use a recursive helper: (and (= (length lst) 3) (all-three-branching? lst)) (define (all-three-branching? lst) (cond ((null? lst) #t) ((three-branching? (car lst)) (all-three-branching? (cdr lst))) (else #f))) But you can't use THREE-BRANCHING? itself as the helper: ... (three-branching? (cdr lst)) ... ;; wrong! If LST is three-branching, then (CDR LST) has two elements, and therefore /isn't/ three-branching, even if its elements are! If you didn't think to extend the domain as described above, then the testing of the three elements is a little more complicated: (and (= (length lst) 3) (or (not (list? (car lst))) (three-branching? (car lst))) (or (not (list? (cadr lst))) (three-branching? (cadr lst))) (or (not (list? (caddr lst))) (three-branching? (caddr lst)))) Of course the OR could be put in a helper procedure, to shorten this code. Scoring: 8: Perfect. 6: Miss atomic case. 4: MAP solution, forgets to process #T and #Fs. 2: Assume 2-level list.
If you don't like your grade, first check these solutions. If we graded your paper according to the standards shown here, and you think the standards were wrong, too bad -- we're not going to grade you differently from everyone else. If you think your paper was not graded according to these standards, bring it to Brian or your TA.