callcc


callcc(fn)
      

Callcc obtains its continuation and passes it to the function fn, which should accept one argument. The continuation itself is represented by a function. Each time this function is applied to zero or more values, it returns the values to the continuation of the callcc application. That is, when the continuation function is called, it returns its arguments as the values of the application of callcc.

If fn returns normally when passed the continuation function, the values returned by callcc are the values returned by fn.

Continuations allow the implementation of nonlocal exits, backtracking, coroutines, and multitasking.

Examples:

The following example implements a simple coroutine mechanism where a function is allowed to "restart" another function from the point it was called:


let k = false

function a()
{ showln('a:1)
  showln(callcc(^(c) { k = c; b() }))
  showln('a:2) }

function b()
{ showln('b:1)
  k("hello from b") }

a()
//> a:1
    b:1
    hello from b
    a:2
      

Also see:

dynamic_wind
values
call_with_values


Core Module Index | Contents