1 / 15

6.001 SICP Sections 5 & 6 – Oct 5, 2001

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”.

zorana
Download Presentation

6.001 SICP Sections 5 & 6 – Oct 5, 2001

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 6.001 SICPSections 5 & 6 – Oct 5, 2001 • Quote & symbols • Equality • Quiz 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… 6.001 SICP

  3. 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

  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) ? 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.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)) ==> #fWhy does eq? return false? 6.001 SICP

  7. (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

  8. (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

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

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

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

More Related