How can this list be enumerated by an iterator of a circuit using call-with-current-continue?

I am trying to read this code:

(define list-iter (lambda (a-list) (define iter (lambda () (call-with-current-continuation control-state))) (define control-state (lambda (return) (for-each (lambda (element) (set! return (call-with-current-continuation (lambda (resume-here) (set! control-state resume-here) (return element))))) a-list) (return 'list-ended))) iter)) 

Can someone explain how call-with-current-continuation works in this example?

thanks

+4
source share
3 answers

The essence of call-with-concurrent-continuation , or call/cc for short, is the ability to capture control points or continuations during program execution. You can then return to these control points by applying them as functions.

Here is a simple example where continuation is not used:

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

If you're not using a sequel, it's hard to tell the difference. Here are a few where we actually use it:

 > (call/cc (lambda (k) (+ 2 (k 3)))) 3 > (+ 4 (call/cc (lambda (k) (+ 2 3)))) 9 > (+ 4 (call/cc (lambda (k) (+ 2 (k 3))))) 7 

When a continuation is called, the control flow returns to where the continuation was captured by call/cc . Recall the call/cc expression as a hole that fills with what is passed in k .

list-iter is a significantly more complex use of call/cc and can be a difficult place to use it. First, here is a usage example:

 > (define i (list-iter '(abc))) > (i) a > (i) b > (i) c > (i) list-ended > (i) list-ended 

Here is an outline of what is happening:

  • list-iter returns a procedure with no i arguments.
  • When i is called, we immediately take the continuation and pass it to the control-state . When this continuation associated with return is called, we will immediately return to the one who called i .
  • For each element in the list, we take a new continuation and rewrite the definition of control-state this new continuation, which means that we will resume the next step 2 from there.
  • After setting up the control-state the next time, we pass the current list item back to return continue, resulting in a list item.
  • When i is called again, repeat from step 2 until for-each does its job for the entire list.
  • Call return with 'list-ended . Since control-state not updated, it will return 'list-ended each time i is called.

As I said, this is a rather complicated use of call/cc , but I hope this is enough to go through this example. For a softer introduction, I would recommend choosing The Seasoned Schemer .

+3
source

Basically, it takes the function f as its parameter and applies f to the current context / state of the program.

From Wikipedia:
(define (f return)
(return 2)
3)

(display (f (lambda (x) x))) ; displays 3

(display (call-with-current-continuation f)) ; displays 2

Thus, basically, when f is called without continuing the current (cc), the function is applied to 2, and then returns 3. When using the continuation of the current, the parameter is applied to 2, which causes the program to go to the point where the current continuation was called, and, thus returns 2. It can be used to generate returns or to suspend the execution flow.

If you know C, think of it this way: in C you can take a pointer to a function. You also have a return mechanism. Suppose that the return has accepted a parameter of the same type that the function performs. Suppose you could take your address and store that address in a variable or pass it as a parameter and let the function return for you. It can be used to simulate throw / catch or as a mechanism for coroutines.

0
source

This is essentially:

 (define (consume) (write (call/cc control))) (define (control ret) (set! ret (call/cc (lambda (resume) (set! control resume) (ret 1)))) (set! ret (call/cc (lambda (resume) (set! control resume) (ret 2)))) (set! ret (call/cc (lambda (resume) (set! control resume) (ret 3))))) (consume) (consume) (consume) 

Hope this is easier to understand.

0
source

Source: https://habr.com/ru/post/1347657/


All Articles