SLIDE 1 A-list example
’((Course 105) (Building Robinson) (Instructor Fisher))) Robinson
- > (val ksf (bind ’Office ’Halligan-242
(bind ’Courses ’(105) (bind ’Email ’comp105-staff ’())))) ((Email comp105-staff) (Courses (105)) (Office Halligan-242))
Halligan-242
- > (find ’Favorite-food ksf)
()
SLIDE 2
Laws of assocation lists
(find k (bind k v l)) = v (find k (bind k’ v l)) = (find k l), provided k != k’ (find k ’()) = ’() --- bogus!
SLIDE 3
Introduce local names into environment
(let ((x1 e1) ... (xn en)) e)
SLIDE 4
Syntax McCarthy should have used
(let ((val x1 e1) ... (val xn en)) e)
SLIDE 5 Function escapes!
- > (define to-the-n-minus-k (n k)
(let ((x-to-the-n-minus-k (lambda (x) (- (exp x n) k)))) x-to-the-n-minus-k))
- > (val x-cubed-minus-27 (to-the-n-minus-k 3 27))
- > (x-cubed-minus-27 2)
- 19
SLIDE 6 No need to name the escaping function
- > (define to-the-n-minus-k (n k)
(lambda (x) (- (exp x n) k)))
- > (val x-cubed-minus-27 (to-the-n-minus-k 3 27))
- > (x-cubed-minus-27 2)
- 19
SLIDE 7
The zero-finder
(define findzero-between (f lo hi) ; binary search (if (>= (+ lo 1) hi) hi (let ((mid (/ (+ lo hi) 2))) (if (< (f mid) 0) (findzero-between f mid hi) (findzero-between f lo mid))))) (define findzero (f) (findzero-between f 0 100))
SLIDE 8 Cube root of 27 and square root of 16
- > (findzero (to-the-n-minus-k 3 27))
3
- > (findzero (to-the-n-minus-k 2 16))
4
SLIDE 9
Lambda questions
(define combine (p? q?) (lambda (x) (if (p? x) (q? x) #f))) (define divvy (p? q?) (lambda (x) (if (p? x) #t (q? x)))) (val c-p-e (combine prime? even?)) (val d-p-o (divvy prime? odd?)) (c-p-e 9) == ? (d-p-o 9) == ? (c-p-e 8) == ? (d-p-o 8) == ? (c-p-e 7) == ? (d-p-o 7) == ?
SLIDE 10
Lambda answers
(define combine (p? q?) (lambda (x) (if (p? x) (q? x) #f))) (define divvy (p? q?) (lambda (x) (if (p? x) #t (q? x)))) (val c-p-e (combine prime? even?)) (val d-p-o (divvy prime? odd?)) (c-p-e 9) == #f (d-p-o 9) == #t (c-p-e 8) == #f (d-p-o 8) == #f (c-p-e 7) == #f (d-p-o 7) == #t
SLIDE 11 An “escaping” function
- > (define to-the-n-minus-k (n k)
(lambda (x) (- (exp x n) k)))
SLIDE 12 Closures represent escaping functions
Function value representation:
( jx :e ;
)
(
binds the free variables of e)
A closure is a heap-allocated record containing
- a pointer to the code
- an environment storing free variables
( j ; fn 7! 3 ;k 7! 27 g j )
code for x-to-the-n-minus-k
SLIDE 13
What’s the closure for conjunction?
(define combine (p? q?) (lambda (x) (if (p? x) (q? x) #f)))
SLIDE 14
Closure for conjunction
( j ; fp ? 7! ;q ? 7! g j )
code for (lambda(x)(if ...))
SLIDE 15 Functions create new functions
- > (define o (f g) (lambda (x) (f (g x))))
- > (define even? (n) (= 0 (mod n 2)))
- > (val odd? (o not even?))
- > (odd? 3)
#t
#f
SLIDE 16 Classic functional technique: Currying
- > (val positive? (lambda (y) (< 0 y)))
- > (positive? 3)
#t
- > (val <-c (lambda (x) (lambda (y) (< x y))))
- > (val positive? (<-c 0)) ; "partial application"
- > (positive? 0)
#f
SLIDE 17
What’s the algebraic law for ‘curry‘?
... (curry f) ... = ... f ... Keep in mind: All you can do with a function is apply it! (((curry f) x) y) = f (x, y)
SLIDE 18 No need to curry by hand!
;; curry : binary function -> value -> function
(lambda (f) (lambda (x) (lambda (y) (f x y)))))
- > (val positive? ((curry <) 0))
- > (positive? -3)
#f
#t
SLIDE 19 Exercises
((curry +) 3) ’(1 2 3 4 5)) ???
- > (exists? ((curry =) 3) ’(1 2 3 4 5))
???
((curry >) 3) ’(1 2 3 4 5)) ??? ; tricky
SLIDE 20 Answers
((curry +) 3) ’(1 2 3 4 5)) (4 5 6 7 8)
- > (exists? ((curry =) 3) ’(1 2 3 4 5))
#t
((curry >) 3) ’(1 2 3 4 5)) (1 2)
SLIDE 21 Bonus content: vulnerable variables?
- > (val seed 1)
- > (val rand (lambda ()
(set seed (mod (+ (* seed 9) 5) 1024)))))
14
131
1
14
SLIDE 22 Bonus: Lambda as abstraction barrier!
- > (val mk-rand (lambda (seed)
(lambda () (set seed (mod (+ (* seed 9) 5) 1024))))))
- > (val rand (mk-rand 1))
- > (rand)
14
131
error: set unbound variable seed
160