The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof: '(abc 123) =...
-
Upload
brian-underwood -
Category
Documents
-
view
220 -
download
0
description
Transcript of The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof: '(abc 123) =...
![Page 1: The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof: '(abc 123) = (cons 'abc (cons 123 '())) '() is a list, so (cons 123.](https://reader036.fdocuments.us/reader036/viewer/2022082415/5a4d1b087f8b9ab059989003/html5/thumbnails/1.jpg)
The “Beauty” of Scheme: Programs as Proofs
Q: Is '(abc 123) a list? A: Yes Proof:
'(abc 123) = (cons 'abc (cons 123 '()))
'() is a list, so (cons 123 '()) is also a list
(cons 123 '()) is a list, so
(cons 'abc (cons 123 '()))
is also a list, Q.E.D.
![Page 2: The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof: '(abc 123) = (cons 'abc (cons 123 '())) '() is a list, so (cons 123.](https://reader036.fdocuments.us/reader036/viewer/2022082415/5a4d1b087f8b9ab059989003/html5/thumbnails/2.jpg)
The Beauty of Scheme: Programs as Definitions
Base-case: fac(0) = 1 Induction: fac(n) = n * fac(n-1)
Factorial: program
(define fac (lambda(n) (if (eq? n 0) 1 (* n (fac (- n 1)))))
Factorial: definition
![Page 3: The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof: '(abc 123) = (cons 'abc (cons 123 '())) '() is a list, so (cons 123.](https://reader036.fdocuments.us/reader036/viewer/2022082415/5a4d1b087f8b9ab059989003/html5/thumbnails/3.jpg)
The Power of Scheme: Functions as First-Class Objects
; apply a function to each number in a list(define map (lambda (fn ln) ; function, list of numbers (if (null? ln) ; null? means equals '() '() (cons (fn (car ln)) ; apply fn to first elmt (map fn (cdr ln)))))) ; recur on rest
> (map square '(1 2 3 4 5))(1 4 9 16 25)
![Page 4: The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof: '(abc 123) = (cons 'abc (cons 123 '())) '() is a list, so (cons 123.](https://reader036.fdocuments.us/reader036/viewer/2022082415/5a4d1b087f8b9ab059989003/html5/thumbnails/4.jpg)
Shortcuts: cond(define (sign x) (if (< x 0) -1 (if (> x 0) 1 0)))
(define (sign x) (cond ((< x 0) -1) ((> x 0) 1) (else 0))) ; sensible default
![Page 5: The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof: '(abc 123) = (cons 'abc (cons 123 '())) '() is a list, so (cons 123.](https://reader036.fdocuments.us/reader036/viewer/2022082415/5a4d1b087f8b9ab059989003/html5/thumbnails/5.jpg)
Shortcuts: list-ref
If ls is a list then
(car ls) = (list-ref ls 0)(car (cdr ls)) = (list-ref ls 1)(car (cdr (cdr ls)) = (list-ref ls 2)
(Like Java/C++ arrays)
![Page 6: The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof: '(abc 123) = (cons 'abc (cons 123 '())) '() is a list, so (cons 123.](https://reader036.fdocuments.us/reader036/viewer/2022082415/5a4d1b087f8b9ab059989003/html5/thumbnails/6.jpg)
Shortcuts: car, cdr
(caar ls) = (car (car ls))(cadr ls) = (car (cdr ls))(cdadr ls) = (cdr (car (cdr ls)))
(etc.)
![Page 7: The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof: '(abc 123) = (cons 'abc (cons 123 '())) '() is a list, so (cons 123.](https://reader036.fdocuments.us/reader036/viewer/2022082415/5a4d1b087f8b9ab059989003/html5/thumbnails/7.jpg)
Comparison with Python/Java/C++
Binding instead of assignment:
(define a 5) vs. a = 5;
![Page 8: The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof: '(abc 123) = (cons 'abc (cons 123 '())) '() is a list, so (cons 123.](https://reader036.fdocuments.us/reader036/viewer/2022082415/5a4d1b087f8b9ab059989003/html5/thumbnails/8.jpg)
Comparison with Python/Java/C++
Recursion instead of loops:int sum(int n) { // sum from 0 to n int s = 0; for (int i=1; i<=n; ++i) { s += i; } return s;}
(define sum (lambda (n) (if (eq? n 0) 0 (+ n (sum (- n 1)))))
![Page 9: The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof: '(abc 123) = (cons 'abc (cons 123 '())) '() is a list, so (cons 123.](https://reader036.fdocuments.us/reader036/viewer/2022082415/5a4d1b087f8b9ab059989003/html5/thumbnails/9.jpg)
Interpreter: The Read/Eval/Print Loop
> 1
1> (+ 3 2)5> ((lambda(x) (* x x)) 4)16
![Page 10: The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof: '(abc 123) = (cons 'abc (cons 123 '())) '() is a list, so (cons 123.](https://reader036.fdocuments.us/reader036/viewer/2022082415/5a4d1b087f8b9ab059989003/html5/thumbnails/10.jpg)
Getting Started
We'll use DrScheme (a.k.a. PLT Scheme, a.k.a. DrRacket):
Runs under Unix, Mac, Windows
Integrated with EOPL
Nice help environment
At Unix prompt, type drscheme, or download from www.drscheme.org
Help / About DrScheme / Take A Tour
Try some examples from lecture:
> (load "filename.scm")