150 likes | 161 Views
6.001 SICP Sections 5 & 6 – Oct 5, 2001. Quote & symbols Equality Quiz. Quote. Symbolic algebra, e.g. “the morning star” is “the evening star”, “the morning star” is “venus” ==> “the evening star” is “venus”.
E N D
6.001 SICPSections 5 & 6 – Oct 5, 2001 • Quote & symbols • Equality • Quiz 6.001 SICP
Quote Symbolic algebra, e.g. “the morning star” is “the evening star”, “the morning star” is “venus” ==> “the evening star” is “venus”. Use symbols to build a language about computation, i.e. a programming language, i.e. scheme… 6.001 SICP
Quote • 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)) 6.001 SICP
Quote vs. List When to use list and when to use quote ? What is the difference between: • (list 1 2 3) • ‘(1 2 3) or between, with (define x 20) • (list (+ x 1) (+ x 2) (+ x 3)) • ‘((+ x 1) (+ x 2) (+ x 3)) Given (define x 20), what is (+ ‘x 5) ? 6.001 SICP
Quote • Draw box and pointer diagram for (list ‘a ‘b ‘c) (cons 'a '(b)) (cons '(a) '(b)) (list 'a 'b) (list '(a) '(b)) (append '(a) '(b)) 6.001 SICP
Recitation problem • What is the value printed if you evaluate the following two expressions (list (+ 3 4) '(+ 3 4) '(list 3 4)) ==> ?? ''x ==> ?? • You observe the following behavior (cons 1 nil) ==> (1) (list 1) ==> (1) (eq? (cons 1 nil) (list 1)) ==> #fWhy does eq? return false? 6.001 SICP
(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 '+)) Drill • what will the following expressions print? (list 'a b) (a 4) '(a b) (a b) (cons b d) (4 a b) (list 'b c) (b (5 6)) (car d) a 6.001 SICP
(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 '+)) Drill • what will the following expressions print? ((car p) 3 4) (3 . 4) ((cadr p) 3 4) 7 ((car r) 3 4) error -- can't apply a symbol ((cadr q) 3 4) error -- can't apply a symbol (car ''a) quote 6.001 SICP
Symbols vs. strings • Strings are for data, input/output messages, etc • String literals such as “help” is not a symbol • Rather they are self-evaluating expressions (like numbers) • Symbols occupy constant space and can be compared in constant time no matter how long their printed representation is 6.001 SICP
Eq? Equal? and = Informally, • = tests if two numbers are the same • equal? tests if two expressions have same printed representation • eq? tests if two symbols are equal also tests if two pairs are “identical” e.g., came from the same cons operation / occupies the same memory location… 6.001 SICP
equality (define x (list 1 2)) (define y (list x x)) (define z (list (list 1 2) (list 1 2)) (equal? (car y) (cadr y)) ==> #t (eq? (car y) (cadr y)) ==> #t (equal? (car z) (cadr z)) ==> #t (eq? (car z) (cadr z)) ==> #f 6.001 SICP
equal? Write the function equal? that takes two trees of symbols and returns true if the same symbols are arranged in the same structure. (equal? '(this is a list) '(this is a list)) ;Value: #t (equal? '(this (is a) list) '(this (is a) list)) ;Value: #t (equal? '(this is a list) '(this (is a) list)) ;Value: #f 6.001 SICP
equal? (define (equal? a b) (or (and (null? a) (null? b)) (and (symbol? a) (symbol? b) (eq? a b)) (and (pair? a) (pair? b) (equal? (car a) (car b)) (equal? (cdr a) (cdr b))))) 6.001 SICP
Self-printer Write a scheme expression that print itself. Tips: 1 - copy the expression, 2 - implement the copier 6.001 SICP
Self-printer ((lambda (x) (list x (list (quote quote) x))) (quote (lambda (x) (list x (list (quote quote) x))))) 6.001 SICP