[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Assignments] [Labs]
Back to Continuations, Continued. On to Continuations, Nearly Concluded.
Held Friday, February 12
Summary
Contents
Handouts
Notes
http://www.unixguru.com''.
;;; Compute x^n for nonnegative integer n.
(define (exp x n)
(cond
((= n 0) 1)
((even? n) (lambda (x) (* x x) (exp x (/ n 2))))
((odd? n) (* x (exp x (- n 1))))))
;;; Compute acc*(x^n) for nonnegative integer n. (define (expacc x n acc) ...)
acc (since 1*a is a).
((odd? n) (expacc x n (* acc x)))
call/cc.
(call/cc fun) calls fun with the
current continuation.
seta, that gets the
next value from the list input
> (define (get)
(let ((result (car input)))
(begin
(set! input (cdr input))
result)))
> (set! input '(1 2 3))
> (get)
1
> (get)
2
> (get)
3
(- (get) (get))? (Assuming that
input is currently (1 2 3).)
(get) first.
We could make that explicit.
> ((lambda (second) (- (get) second)) (get))
(get), we
might as well provide one for the first.
> (set! input '(1 2 3))
> ((lambda (b)
((lambda (a)
(- a b))
(get)))
(get))
1
(get) first,
we might write
> (set! input '(1 2 3))
> ((lambda (a)
((lambda (b)
(- a b))
(get)))
(get))
-1
(define (factorial n)
(letrec
((factcps (lambda (n cont)
(if (= n 0) (cont 1)
(factcps (- n 1)
(lambda (subfact)
(cont (* n subfact))))))))
(factcps n (lambda (n) n))))
History
Back to Continuations, Continued. On to Continuations, Nearly Concluded.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Assignments] [Labs]
Disclaimer Often, these pages were created ``on the fly'' with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS302/99S/Outlines/outline.09.html
Source text last modified Mon Feb 15 10:05:07 1999.
This page generated on Thu Feb 25 15:45:44 1999 by SiteWeaver. Validate this page's HTML.
Contact our webmaster at rebelsky@math.grin.edu