CS-Fall 2010 MT1 Solutions

# CS-Fall 2010 MT1 Solutions - 1 What will Scheme...

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

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.

Unformatted text preview: 1. What will Scheme print? > (define (poof x) (lambda (y) (+ x y)) ) > (define bam 10) > ((poof bam) 4) __14__ A better name for POOF would be MAKE-ADDER; (poof bam) returns a function we might call +10. Then we pass 4 to that function and get 14. > (define swoosh (lambda (num) (lambda (y) (+ y num)) )) > (define kablooie 10) > ((swoosh kablooie) 4) __14__ The trick here is that it's pretty much exactly the same problem as the previous one. SWOOSH is still basically MAKE-ADDER; we just wrote the lambda out explicitly here. > (define (mystery sent) (if (empty? sent) '() (se (first sent) (mystery sent)) )) > (mystery '(you cant always get what you want)) __Infinite loop__ In this case, you had to notice that the recursive call to MYSTERY doesn't actually make SENT any shorter. Since the base case is when the sentence is empty, we'll never reach it! > (define garply 5) > (and (> garply 0) (/ 20 garply)) __4__ This may have been the most-missed problem on the entire exam. The way AND works is it goes through each argument and sees if it's false. If so, it just stops there. Otherwise, it returns the value of the /last/ argument. We're pretty sure this came up in the book or lab, but it threw a number of people. Many people put #t as the result, but since any non-#f value counts as true, AND can get away with the behavior it actually has. Some people said this was an error, presumably because (/ 20 garply) wasn't #t or #f. Again, in Scheme any value other than #f counts as true! > (define brrrm '((we need to) (go (deeper)))) > (cdar brrrm) 10/24/2010 https://inst.eecs.berkeley.edu/~cs61a/fa… inst.eecs.berkeley.edu/~cs61a/…/mt1 1/9 __(need to)__ The most common mistake here was to read this as (cadr brrrm) instead of (cdar brrrm). Remember, the As and Ds follow the same order as nesting CARs and CDRs: (cdr (car brrrm)). > (car brrrm) (we need to) > (cdr (car brrrm)) (need to) Scoring: 1pt each, all or nothing. 2. Fill-in-the-Blanks / Box-and-Pointer Diagrams > (LIST 'a '(b . c) #f) (a (b . c) #f) +---+---+ +---+---+ +---+---+-->| o | o---->| o | o---->| o | / | +-|-+---+ +-|-+---+ +-|-+---+ | | | v v v a +---+---+ #f | o | o | +-|-+-|-+ | | v v b c This wasn't too hard to pick a procedure for, since CONS only takes two arguments and APPEND only takes lists. The box-and-pointer diagram wasn't too hard either: the list has three elements, so we draw three "spine" pairs. Then we fill in the elements. A and #f are easy. (b . c) is a single pair, which is also not too bad. > (CONS '(a) '(b)) ((a) b) +---+---+ +---+---+-->| o | o---->| o | / | +-|-+---+ +-|-+---+ | | v v +---+---+ b | o | / | +-|-+---+ | v a This was a little trickier, but the other list functions wouldn't have 10/24/2010 https://inst.eecs.berkeley.edu/~cs61a/fa… inst.eecs.berkeley.edu/~cs61a/…/mt1 2/9 helped here: > (list '(a) '(b)) ((a) (b)) > (append '(a) '(b)) (a b) What does CONS do? It just makes one new pair whose car is the first argument and whose cdr is the second argument. What's the car of the result?argument and whose cdr is the second argument....
View Full Document

{[ snackBarMessage ]}

### Page1 / 9

CS-Fall 2010 MT1 Solutions - 1 What will Scheme...

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

View Full Document
Ask a homework question - tutors are online