help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: The fundamental concept of continuations


From: Jeff M.
Subject: Re: The fundamental concept of continuations
Date: Tue, 09 Oct 2007 19:27:10 -0000
User-agent: G2/1.0

> (6) any good readable references that explain it lucidly ?

This was something that has been very interesting to me for a while
now, and I'm actually still having a difficult time wrapping my head
around it completely.

The best written explanation that I've come across was in "The Scheme
Programming Language" (http://mitpress.mit.edu/catalog/item/
default.asp?ttype=2&tid=9946). But perhaps others have better
references.

I'll attempt my own little explanation of call/cc. I'll butcher some
of it, I'm sure, but hopefully those more knowledgeable will politely
correct me. I will start with a loose analogy and point out a couple
examples I came across that did make a lot of sense.

First, the bad analogy I have (if you are coming from C programming
like me) is setjmp and longjmp. This is a bad analogy in that you're
talking about hardware and stack states as opposed to functions, but a
good analogy in that it saves the current state of execution, and
returns to that same state at a later time with a piece of data
attached to it.

My first example of using this would be to create a return function in
Scheme. I hope I don't get this wrong, but the example would be
something like this:

(define (my-test x)
  (call/cc (lambda (return)
             (return x))))

Now, here's my understanding of what is happening under-the-hood:

1. call/cc stores the current execution state and creates a function
to restore to that state.

2. call/cc then calls its own argument with the function it created.

The key here is that "return" is a function (created by call/cc)
taking 1 argument, and it restores execution at the same state it was
when the call/cc began (or immediately after it?). This line:

(return x)

is really just calling the function created by call/cc, which will
restore the execution state to what it was just prior to the call/cc,
along with a parameter (in this case, the value of x).

My next example I don't follow 100%, and I won't attempt to reproduce
it here, but it generates a continuation that modifies itself (bad?)
to define a list iterator.

http://blog.plt-scheme.org/2007/07/callcc-and-self-modifying-code.html

I recommend putting that code into a Scheme interpreter and running
it. You'll get it.

Hope this helps, and I look forward to better explanations than mine
that will help me along as well. :)

Jeff M.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]