From: "Jeff M." <massung@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: The fundamental concept of continuations
Date: Tue, 09 Oct 2007 19:27:10 -0000 [thread overview]
Message-ID: <1191958030.300166.209950__39170.7524461255$1192017387$gmane$org@57g2000hsv.googlegroups.com> (raw)
In-Reply-To: <1191906949.179197.217470@57g2000hsv.googlegroups.com>
> (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.
next prev parent reply other threads:[~2007-10-09 19:27 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1191906949.179197.217470@57g2000hsv.googlegroups.com>
2007-10-09 5:59 ` The fundamental concept of continuations Barb Knox
2007-10-09 6:07 ` Bakul Shah
2007-10-09 6:09 ` .
[not found] ` <see-36543E.18592809102007@lust.ihug.co.nz>
2007-10-09 6:24 ` gnuist006
[not found] ` <1191911045.604596.50110@19g2000hsx.googlegroups.com>
2007-10-09 13:21 ` Joel J. Adamson
2007-10-14 22:56 ` Lawrence D'Oliveiro
2007-10-15 4:17 ` George Neuner
[not found] ` <470b1aa6$0$79886$742ec2ed@news.sonic.net>
2007-10-09 6:33 ` gnuist006
[not found] ` <1191911604.169846.244430@r29g2000hsg.googlegroups.com>
2007-10-09 7:15 ` Bakul Shah
2007-10-09 7:11 ` Peter Danenberg
2007-10-09 12:05 ` Matthias Benkard
[not found] ` <470b1b30$0$11022$4c368faf@roadrunner.com>
2007-10-09 6:34 ` gnuist006
[not found] ` <1191911662.983658.79540@g4g2000hsf.googlegroups.com>
2007-10-09 10:00 ` Tim Bradshaw
[not found] ` <1191924032.449463.13290@22g2000hsm.googlegroups.com>
2007-10-09 10:13 ` Diez B. Roggisch
2007-10-09 12:50 ` Matthias Blume
2007-10-09 13:50 ` josephoswald+gg@gmail.com
2007-10-09 19:20 ` gnuist006
[not found] ` <m2641g1mgn.fsf@my.address.elsewhere>
2007-10-09 19:37 ` gnuist006
2007-10-09 20:41 ` Chung-chieh Shan
2007-10-10 0:47 ` Matthias Blume
[not found] ` <ok5tt4-2ij.ln1@mantle.rutgers.edu>
2007-10-10 4:52 ` Marlene Miller
[not found] ` <1191957606.187050.272820@v3g2000hsg.googlegroups.com>
2007-10-09 20:32 ` .
2007-10-09 18:18 ` George Neuner
[not found] ` <m5ang3h11oorvsrkit4q1moi6mirofbdbr@4ax.com>
2007-10-09 19:24 ` gnuist006
2007-10-09 19:27 ` Jeff M. [this message]
2007-10-10 4:39 ` Marlene Miller
[not found] ` <8IYOi.658994$p47.379449@bgtnsc04-news.ops.worldnet.att.net>
2007-10-10 4:46 ` Marlene Miller
2007-10-10 7:11 ` Dmitri Minaev
2007-10-10 10:49 ` David Kastrup
[not found] ` <85zlyrutux.fsf@lola.goethe.zz>
2007-10-11 7:18 ` George Neuner
[not found] ` <rqhrg3d2thcqaip790k1eb6t8cdk2g8d8v@4ax.com>
2007-10-12 19:17 ` David Kastrup
[not found] ` <85lka8p2h4.fsf@lola.goethe.zz>
2007-10-13 3:11 ` Rob Warnock
2007-10-13 3:13 ` Paul Rubin
[not found] ` <1191931524.047907.98040@v3g2000hsg.googlegroups.com>
2007-10-13 23:14 ` Alex Martelli
2007-10-09 5:15 gnuist006
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='1191958030.300166.209950__39170.7524461255$1192017387$gmane$org@57g2000hsv.googlegroups.com' \
--to=massung@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).