pdcawley at bofh.org.uk
Thu Mar 15 06:27:50 PDT 2007
benjohn at fysh.org writes:
>> So, on the train back on Wednesday I thought of a way of explaining
>> continuations. This morning, I finally wrote it up as a blog post:
>> Please let me know if it helps.
> Erm - I'm not sure that it helps me, and I thought I knew what they
> were, sorry! I did find your explaination based on your Sudoku solver
> compelling though.
> I think I'd have preffered an explaination based no that (but I wasn't
> at the meeting :-)). I don't quite see why the search couldn't throw out
> exceptions then it fails, and so return up and try out another possible
If you use exceptions, you have to keep track of which alternatives
you've tried, and they don't get thrown to the right place, if that
makes sense. Continuations help enormously because they help
encapsulate the bookkeeping (they're also useful for handling
backtracking of your state)
> Perhaps because you want to huristically choose which search path
> to take, so you kind of need to be able to prioritse "threads" of
> possible solutions?
That too. Again, having my grid reset its state a branch fails is very
useful here. Before I implemented that I was pretty much restricted to
going through the grid in a fixed order; switching to a 'pick the most
constrained cell and try and give it a value' strategy substantially
improved the solver's speed. The current 'example' sudoku I'm solving
only calls the failure continuation six times.
> I've also heard of continuation based web servers. Perhaps these could
> be a good way of explaining continuations too?
The trouble with using a continuation based webserver (say) as your
point of entry to explaining how continuations work is that, if you
get the implementation right, you never actually see the continuations
when you're writing your webapp; you just see (probably) a new control
structure (actually, what you tend to see is a *lack* of a control
structure because the continuations take care of it in the background
and you don't have to write all that explicit control logic).
The thinking behind the blog post was based on the idea that
continuations are, in a sense, the ur-control structure; once you have
continuations you can use them to implement pretty much all the
control structures you need, which is why I gave examples of using
continuations to replace return, break, next and redo. It's not that
it's a sensible thing to do with them (after all, we already *have*
return, break, next and redo so why reinvent), just that they are
things that people already (I hope) understand.
Piers Cawley <pdcawley at bofh.org.uk>
More information about the Chat