6.001 Recitation #8 – Feb 28,
2003
RI: Konrad
Tollmar
•Quote & symbols
•Equality
• Quote evaluation rules:
(quote (a b))==> (list (quote a)
(quote b))
(quote <self-eval-expr>) ==> <self-eval-expr>
‘(a b) == (quote (a b))
1. Draw box and pointer diagram and printed
representation for:
'(a b)
(cons 'a '(b))
(list 'a 'b)
(list '(a) '(b)
)
(cons '(a) '(b))
2. Equality
Given:
(define x (list 1 2))
(define y (list x x))
(define z (list (list 1 2) (list 1 2))
Evaluate:
(equal? (car y) (cadr y))
(eq? (car y) (cadr y))
(equal? (car z) (cadr z))
(eq? (car z) (cadr z))
3. Search for a symbol
Write the function memq that takes two arguments, one list of symbols and one single symbol, and returns true if single symbol is within the list of symbols.
4. Compare to lists
Write the function equal? that takes two trees of symbols and returns true if the same symbols are arranged in the same structure.
5. Drill, What will the following expressions print?
(define
a 3)
(define
b 4)
(define
c (list 5 6))
(define
d '(a b))
(define
p (list cons +))
(define
q '(cons +))
(define r (list 'cons '+))
(list 'a b)
'(a
b)
(cons
b d)
(list
'b c)
(car
d)
((car
p) 3 4)
((cadr
p) 3 4)
((car
r) 3 4)
((cadr
q) 3 4)
(car
''a)