Download (quote ())

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
6.001 SICP
Sections 5 & 6 – Oct 5, 2001
• Quote & symbols
• Equality
• Quiz
6.001 SICP
1
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
2
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
3
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
4
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
5
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?
6.001 SICP
6
Drill
(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 '+))
• 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)
6.001 SICP
a
7
Drill
(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 '+))
• 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)
6.001 SICP
quote
8
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
9
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
10
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))
6.001 SICP
==> #f
11
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
12
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
13
Self-printer
Write a scheme expression that print itself.
Tips: 1 - copy the expression, 2 - implement the copier
6.001 SICP
14
Self-printer
((lambda (x)
(list x (list (quote quote) x)))
(quote (lambda (x)
(list x (list (quote quote) x)))))
6.001 SICP
15
Related documents