Skip to content
Bill Hails edited this page Jun 30, 2024 · 12 revisions

The special construct here takes a function of one argument and passes it the current continuation as a function of one argument, so that calling that argument function is equivalent to returning through here. An example may make this clearer (or might not, continuations are notoriously tricky to get your head around at first 😁)

let
    fn funky(k) { k(4) }
in
    3 + here fn(k) {
        if (funky(k)) {
            5
        } else {
            6
        }
    }

returns 7 (3 + 4). So the here invokes the anonymous function with a continuation k, and the anonymous function calls funky in the conditional of an if statement. But because funky calls its continuation with argument 4 this value pops out as the value of the here, bypassing the rest of the anonymous function, and that 4 gets added to 3 and printed: 7.

If that's still not clear, try mentally renaming k to throw in the example above. Although it's not the same thing as a try/catch, it reasonably explains the behaviour in that example.

A continuation is first class like any other function and can be passed to other functions, stored in variables, called later etc. Simply put, continuations are first-class return statements.

The type of here is ((#a -> #_) -> #a) -> #a (see Type Notation for a discussion of ->).

here is called call-with-current-continuation, or call/cc in other languages.

Next: Then and Back

Clone this wiki locally