Presentation is loading. Please wait.

Presentation is loading. Please wait.

6.001 SICP 1 6.001 SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.

Similar presentations


Presentation on theme: "6.001 SICP 1 6.001 SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz."— Presentation transcript:

1 6.001 SICP 1 6.001 SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz

2 6.001 SICP 2 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…

3 6.001 SICP 3 Quote quote evaluation rules: (quote (a b)) ==> (list (quote a) (quote b)) (quote ()) ==> nil (quote ) ==> quote syntactic sugar: ‘(a b) == (quote (a b))

4 6.001 SICP 4 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) ?

5 6.001 SICP 5 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 6.001 SICP 6 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)) ==> #f Why does eq? return false?

7 6.001 SICP 7 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 (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 '+))

8 6.001 SICP 8 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 (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 '+))

9 6.001 SICP 9 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

10 6.001 SICP 10 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…

11 6.001 SICP 11 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

12 6.001 SICP 12 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

13 6.001 SICP 13 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)))))

14 6.001 SICP 14 Self-printer Write a scheme expression that print itself. Tips: 1 - copy the expression, 2 - implement the copier

15 6.001 SICP 15 Self-printer ((lambda (x) (list x (list (quote quote) x))) (quote (lambda (x) (list x (list (quote quote) x)))))


Download ppt "6.001 SICP 1 6.001 SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz."

Similar presentations


Ads by Google