Benefits of continuation?

Quinn Dunkan quinn at
Fri Nov 3 19:10:06 CET 2000

On Fri, 03 Nov 2000 10:35:36 GMT, rerwig at <rerwig at> wrote:
>In article <ljt40tg9v67rf4iksb9p2olibo2q36iaur at>,
>  Courageous <jkraska1 at> wrote:
>> On Fri, 03 Nov 2000 08:00:22 GMT, rerwig at wrote:
>> >I think I begin to understand the basics of continuation.
>> >But what I don't understand is how it can help me to create better
>> >code. In other words, what are the benefits on using continuation?
>> The real question is do you have a situation in which you need to
>> keep "restore points" at various times during execution so that you
>> can later go to them? If you don't, you almost certainly do not have
>> any reason to use continuations directly.
>I think this remark above is very true. Context switches was the very
>first brain wave I had. Makes me wonder whether continuation actual ever
>will be used directly. Indirect usage ("under-water", hidden, etc.)
>seems more likely.

A popular use for continuations is to implement "ambiguous" or "multiple
return" values via backtracking.  For instance, in sather, one has 'iterators'
which act like little continuations.  A python-esque translation:

class IterList:
    def __init__(self, data=None):
        if data is None:
   = []
   = data
    def getitem!(self):
        # of course, if we used iterators, 'for' would be an iterator too
        for i in range(len(

a = IterList([1,2,3,4])
b = IterList(['a', 'b', 'c'])
    m = a.getitem!()
    n = b.getitem!()
    print m, n

# prints:
# 1 a
# 2 b
# 3 c
# notice the first fail quits the loop... a more icon-ish way would have
# each b.getitem! fail backtracks to a.getitem! so you'd have:
# 1 a
# 1 b
# 1 c
# 2 a
# 2 b
# 2 c
# etc.

Among other things, this would finally put that whole
'while line = f.readline()' thread to a rest:

    line = fp.getline!()

Notice this sort of thing achieves basically the same sort of result as a lazy
list.  If it seems an awful lot to add to a language just to avoid typing
'if not line: break', there are other things you can do with them :)

Obviously, python isn't going to add a whole bunch of keywords to support this
(it'd have to go remove the old control structures to not accumulate cruft so
it'd be a different language at that point anyway), but something equivalent
could be faked up with continuation objects.  I haven't given it a lot of
thought yet, but I'd like to be able to say something like:

iter = a.iterator()
print iter.val
iter.loop() # bounces back up to 'iter = a.iterator()'

which is probably possible by wrapping continuation objects...

Also, I gather that compiler/interpreter writers are often interested in
passing continuations around "in the buff" to implement things like control
structures and functions in their languages.

More information about the Python-list mailing list