week6 - CS 61A LAB EXERCISES: 2.25. Extract 7 Week 6...

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

View Full Document Right Arrow Icon
CS 61A Week 6 solutions LAB EXERCISES: 2.25. Extract 7 (cadr (caddr '(1 3 (5 7) 9))) I did that one by knowing that "cadr" means "the second element" and "caddr" means "the third element," and the seven is the second element of the third element of the overall list. (car (car '((7))) (cadr (cadr (cadr (cadr (cadr (cadr '(1 (2 (3 (4 (5 (6 7)))))))))))) 2.53. Finger exercises. Note that it matters how many parentheses are printed! > (list 'a 'b 'c) (a b c) > (list (list 'george)) ((george)) > (cdr '((x1 x2) (y1 y2))) ((y1 y2)) > (cadr '((x1 x2) (y1 y2))) (y1 y2) > (pair? (car '(a short list))) #f > (memq 'red '((red shoes) (blue socks))) #f > (memq 'red '(red shoes blue socks)) (red shoes blue socks) 2.55 (car ''abracadabra) When you write 'foo it's just an abbreviation for (quote foo) no matter what foo is, and no matter what the context is. So ''foo is an abbreviation for
Background image of page 1

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

View Full DocumentRight Arrow Icon
(quote (quote foo)) If you enter the expression (car ''abracadabra) you are really saying (car (quote (quote abracadabra))) Using the usual evaluation rules, we start by evaluating the subexpressions. The symbol car evaluates to a function. The expression (quote (quote abracadabra)) evaluates to the unevaluated argument to (the outer) quote, namely (quote abracadabra) That latter list is the actual argument to car, and so car returns the first element of that list, i.e., the word quote. Another example: (cdddr '(this list contains '(a quote))) is the same as (cdddr '(this list contains (quote (a quote)))) which comes out to ((quote (a quote))) P.S.: Don't think that (car ''foo) is a quotation mark! First of all, the quotation mark has already been turned into the list for which it is an abbreviation before we evaluate the CAR; secondly, even if the quotation mark weren't an abbreviation, CAR isn't FIRST, so it doesn't take the first character of a quoted word! 2.27. Deep-reverse. This is a tough problem to think about, although you can write a really simple program once you understand how. One trick is to deep-reverse a list yourself, by hand, without thinking about it too hard, and THEN ask yourself how you did it. It's pretty easy for you to take a list like ((1 2 3) (4 5 6) (7 8 9)) and instantly write down ((9 8 7) (6 5 4) (3 2 1)) How'd you do it? The answer probably is, "I reversed the list and then I
Background image of page 2
deep-reversed each of the sublists." So: (define (deep-reverse lst) ;; Almost working version (map deep-reverse (reverse lst))) But this doesn't QUITE work, because eventually you get down to the level of atoms (symbols or numbers) and you can't map over an atom. So: (define (deep-reverse lst) (if (pair? lst) (map deep-reverse (reverse lst)) lst)) If you tried to define deep-reverse without using map, you'll appreciate the intellectual power it gives you. You probably got completely lost in cars and cdrs, none of which are used in this program. Now that you understand the algorithm, it's possible to do what the problem
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This homework help was uploaded on 04/09/2008 for the course CS 61A taught by Professor Harvey during the Spring '08 term at Berkeley.

Page1 / 15

week6 - CS 61A LAB EXERCISES: 2.25. Extract 7 Week 6...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online