Announcements: See Web Page For Talk Schedule Dire Consequences If I Don't Hear From You by Monday Schedule Next Week
Announcements: See Web Page For Talk Schedule Dire Consequences If I Don't Hear From You by Monday Schedule Next Week
Announcements: See Web Page For Talk Schedule Dire Consequences If I Don't Hear From You by Monday Schedule Next Week
(resumable-fun)
Continuations with multiply
Problem: how to use call/cc with an
argument?
(define (mult-list L)
(call/cc mult-list-main L))
;; this is bad code – can’t take
;; a list
Trick: have call/cc call an anonymous
function
(define (mult-list L)
(call/cc (lambda (escape)
(mult-list L escape)))
Multiply with continuations
(define (mult-list-main L escape)
(cond
((null? L) 1)
((=0 (car L)) escape 0)
(else (* (car L)
(mult-list-main
(cdr L) escape))))
(define (mult-list L)
(call/cc
(lambda (escape)
(mult-list-main L escape)))
Implement your own
continuation
;; con has “to be done” multiplications
(define (mult-list L con)
(cond
((null? L) (con 1))
((= 0 (car L) 0)
(else (mult-list (cdr L)
(lambda (n)
(* n (con (car L)))))))
To actually call the function:
(define (id x) x)
(mult-list ‘(1 2 3) id)