Laser Accent 1
CSSE 304 Day 30
Copyright By PowCoder代写 加微信 powcoder
More call/cc examples
continuations as a datatype
Good and bad code for letrec
Springer/Friedman excerpt to read
CSSE 304 Day 30
More call/cc examples
continuations as a datatype
Starting code for live coding (today and tomorrow) is in live-in-class folder, linked from day 1 of the schedule page
Good and bad code for letrec
Springer/Friedman excerpt to read
Word of the day: reify
List-index
Standard approach:
(define (list-index item L)
[(null? L) -1]
[(eq? (car L) item) 0]
[else (+ 1 (list-index item
(cdr L)))]))
What is the problem with this?
One solution: accumulator approach
But “standard recursion” seems so much more natural!
Can use call/cc to escape with the -1 answer?
Still more call/cc examples
e) (define list-index
(lambda (sym L)
(lambda (answer)
(let loop ([L L])
(cond [(null? L) (answer -1)]
[(eqv? sym (car L)) 0]
[else (+ 1
(loop (cdr L)))]))))))
> (list-index ‘a ‘(b a c))
> (list-index ‘a ‘(b d c))
f) ((car (call/cc list)) (list cdr 1 2 3))
And more call/cc examples
g) (let ([f 0] [i 0])
(call/cc (lambda (k) (set! f k)))
(printf “~a~n” i)
(set! i (+ i 1))
(if (< i 10) (f "ignore")))
h) (define strange1
(lambda (x)
(display 1)
(call/cc x)
(display 2)))
(lambda (k) k)))
“mondo bizarro” example
(define strange2
(lambda (x)
(display 1)
(call/cc x)
(display 2)
(call/cc x)
(display 3)))
(strange2 (call/cc (lambda (k) k)))
We probably will not do this one in class; good practice for you.
Back to writing CPS code
This time we represent continuations by our variant-record datatypes
Recap: Environment representations
Use Scheme procedures as environments
Use environment datatype
(define apply-env
(lambda (env sym)
(env sym)))
(define empty-env
(lambda ()
(lambda (sym)
(eopl:error 'apply-env
"No binding for ~s"
(define extend-env
(lambda (syms vals env)
(lambda (sym)
(let ([pos (list-find-position
(if (number? pos)
(list-ref vals pos)
(apply-env env sym))))))
(define-datatype environment environment?
[empty-env-record]
[extended-env-record
(syms (list-of symbol?))
(vals (list-of scheme-value?))
(env environment?)])
(define empty-env
(lambda ()
(empty-env-record)))
(define extend-env
(lambda (syms vals env)
(extended-env-record syms
(define apply-env
(lambda (env sym)
(cases environment env
[empty-env-record ()
(errorf 'apply-env
"No binding for ~s" sym)]
[extended-env-record (syms vals env)
(let ([pos
(list-find-position sym syms)])
(if (number? pos)
(list-ref vals pos)
(apply-env env sym)))])))
Continutaion representations
Two possibilities
Use Scheme procedures as your continuations (as we have done previously)
Use the continuation datatype
With many variants and a complex apply-k procedure
You should understand both, but you only have to use the continuation datatype in your A18 interpreter (and, yes, you must use it)
Advantages of continuation datatype
You can "see into" the continuations
Thus easier to debug. "trace" will let you see "what's inside" the continuations.
And easier to use this exercise as a means of understanding what continuations are all about.
You can implement continuations in a language that does not have first-class procedures. And more efficiently in a language that does have them.
Advantage of Scheme Procedure Continuations
It's more like what we did with CPS before.
All of the information needed for the continuation is in the procedure definitions, so understanding the code requires less mental "jumping around".
(define read-flatten-print
(lambda ()
(display "enter slist to flatten: ")
(let ([slist (read)])
(unless (eq? slist 'exit)
(flatten-cps slist
(make-k (lambda (val)
(pretty-print val)
(read-flatten-print))))))))
(define flatten-cps
(lambda (ls k)
(if (null? ls)
(apply-k k ls)
(flatten-cps (cdr ls)
(lambda (v) (if (list? (car ls))
(flatten-cps (car ls)
(make-k (lambda (u) (append-cps u v k))))
(apply-k k (cons (car ls) v))))))))
(define append-cps
(lambda (L1 L2 k)
(if (null? L1)
(apply-k k L2)
(append-cps (cdr L1)
(make-k (lambda (appended-cdr)
(apply-k k (cons (car L1)
appended-cdr))))))))
Starting code
Transformations:
Live coding
Details are in a Word document in the SlidesPPT folder. Stating code and solution are in Resources folder (Day 34 as of Spring 2014). Before class only display the starting code.
/docProps/thumbnail.jpeg
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com