lisp - scheme continuations for dummies -


for life of me, can't understand continuations. think problem stems fact don't understand they for. examples i've found in books or online trivial. make me wonder, why want continuations?

here's typical impractical example, tspl, believe quite recognized book on subject. in english, describe continuation "what do" result of computation. ok, that's sort of understandable.

then, second example given:

(call/cc   (lambda (k)     (* 5 (k 4)))) => 4  

how make sense?? k isn't defined! how can code evaluated, when (k 4) can't computed? not mention, how call/cc know rip out argument 4 inner expression , return it? happens (* 5 .. ?? if outermost expression discarded, why write it?

then, "less" trivial example stated how use call/cc provide nonlocal exit recursion. sounds flow control directive, ie break/return in imperative language, , not computation.

and purpose of going through these motions? if needs result of computation, why not store , recall later, needed.

forget call/cc moment. every expression/statement, in programming language, has continuation - is, result. in c, example,

x = (1 + (2 * 3));  printf ("done"); 

has continuation of math assignment being printf(...); continuation of (2 * 3) 'add 1; assign x; printf(...)'. conceptually continuation there whether or not have access it. think moment information need continuation - information 1) heap memory state (in general), 2) stack, 3) registers , 4) program counter.

so continuations exist implicit , can't accessed.

in scheme, , few other languages, have access continuation. essentially, behind back, compiler+runtime bundles information needed continuation, stores (generally in heap) , gives handle it. handle function 'k' - if call function continue after call/cc point. importantly, can call function multiple times , continue after call/cc point.

let's @ examples:

> (+ 2 (call/cc (lambda (cont) 3))) 5 

in above, result of call/cc result of lambda 3. continuation wasn't invoked.

now let's invoke continuation:

> (+ 2 (call/cc (lambda (cont) (cont 10) 3))) 12 

by invoking continuation skip after invocation , continue right @ call/cc point. (cont 10) continuation returns 10 added 2 12.

now let's save continuation.

> (define add-2 #f) > (+ 2 (call/cc (lambda (cont) (set! add-2 cont) 3))) 5 > (add-2 10) 12 > (add-2 100) 102 

by saving continuation can use please 'jump to' whatever computation followed call/cc point.

often continuations used non-local exit. think of function going return list unless there problem @ point '() returned.

(define (hairy-list-function list)   (call/cc     (lambda (cont)        ;; process list ...         (when (a-problem-arises? ...)          (cont '()))         ;; continue processing list ...         value-to-return))) 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -