What is the difference between Scheme sequels and other languages?

Asked

Viewed 205 times

8

I have heard that Scheme has "first-class sequels", while other Lisps (and other languages) do not. I researched it, but the material I found was not specific.

What I understand is that sequels are like callback functions on Node.js, but with a different purpose. For example, no sequel:

(define (ao-quadrado x)
  (* x x))

And continuing:

(define (ao-quadrado x cont)
  (cont (* x x)))

Is there any other difference? I lost something along the way?

2 answers

5

Continuations in Scheme are like increased closures that capture the return stack, the environment and the current run point. In other languages, such as Javascript, continuations are "mere" functions or closures that capture the surrounding environment.

In this way, it is possible, for example, to take a mapping function, which typically calls a function for each element, and make an iterator, which is called to return the next element:

(define (my-hash-iterator a-hash)
  (let ((return-values #f)
        (read-value #f))
    (lambda ()
      (let/cc iteration
        (set! return-values iteration)
        (if read-value
            (read-value)
            (begin
              (hash-table-for-each a-hash
                                   (lambda (key value)
                                     (let/cc cont
                                       (set! read-value cont)
                                       (return-values #t key value))))
              (set! read-value #f)    ; iterador volta ao início se chamado novamente
              (return-values #f #f #f)))))))

The idea is to enter the function given to hash-table-for-each and catch a follow inside, (set! read-value cont), that when invoked, will return it to hash-table-for-each. The outward continuation is to return each of the values at a time.

3


The callback functions of the nodejs are not a particular case of continuations. "Continuation" is a more general term than that.

Translating from wikipedia:

A continuation is a representation abstract of the execution flow state of a program. A continuation reifies implicit flow control in an explicit function

For example, suppose I am doing the following account:

(define (conta)
  (* (+ 1 2) 4))

There is an implicit order of flow control when I perform these operations. I can explain it by converting the program to single assignment form:

a <- 1 + 2
b <- a * 4

Continuations are a way of representing this implicit execution order. Given a point in the code, the continuation of that point is a function that represents what will happen in the future. For example, the continuation of the sum operation point in the third row is a function (lambda (a) (a * 4))

One thing we can do with continuations is to write our program so that the continuations appear explicitly. This is called Continuation Passing Style and that’s what people do in Nodejs. In our example, this would be using operations like +-cps and *-cps instead of + and * that return values:

(define (conta-cps cont)
  (+-cps 1 2 (lambda (a)
    (*-cps a 4 cont)))

Programming like this has some benefits. For example, with continuations you can "pause" code execution. Just take the continuation and save it in some data structure and call it back later. Continuations also allow you to implement some control mechanisms yourself, such as "Return", "Try-catch", and iterators.

The problem is that it is a bag convert all its functions to CPS in hand. This is where first-class sequels come in, using call-with-Current-continuation (call/cc) you can write your code without sequels but you have access to sequels as if you had written the code in CPS.

I won’t give examples of call/cc because I think the code gets too complicated. In practice you can use other less powerful tools that give you similar benf;icios (can pause and restart program execution, etc). For example, in Scheme there are bounded continuations, in Python you have generators and in Lua you have corotins.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.