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 ()) ==> nil
(quote <self-eval-expr>) ==> <self-eval-expr>
• Quote syntactic sugar: ‘(a b) == (quote (a b))
1. Draw box and pointer diagram and printed
representation for:
'(a b)
-> (a b)
(cons 'a '(b))
-> (a b)
(list 'a 'b)
-> (a b)
(list '(a) '(b)
)
-> ((a) (b))
(cons '(a) '(b))
->
((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)) -> #t
(eq? (car y) (cadr y)) -> #t
(equal? (car z) (cadr z)) -> #t
(eq? (car z) (cadr z)) -> #f
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.
(define (memq item lst)
(if (null?
lst) nil (cons (car lst) (memq item (cdr lst)))))
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.
- to be completed next recitation -