05 September 2011

Controlling Flow: Call with Current Continuation

It is my belief that programming concepts one is able to understand and master truly distinguish a coder ability to think and design beautiful code. Learning those concepts is essential for becoming a better programmer. [I will refrain from defining "better programmer".]

Starting easily, do you remember when you tried to understand pointers for the first time? Or when you first heard about object oriented programming? Or all those fancy paradigms or data structures? It's not like you need to use them, but understanding them probably increases your awareness of how things can be done.

Also you can always survive without them, but having those tools readily available on a language allow you to focus on the implementation problem without needing to care on specific rules (e.g. Many people do OOP in pure C and many implement iterators in C++ though there is a clear lack of support for it).

I recently came with a new concept I was not aware of: a new way to control and understand the flow of computation. Well controlling flow can be done with several techniques - there is conditionals and loops for all flavours, there is exceptions and there is generators, there is callbacks, there is message oriented programming style, etc... - and there is continuations!

I came across them while reading on the function call-with-current-continuation from the scheme language, and I couldn't sleep before understanding that new concept.

So I read what I could find on it:

In the end I got that idea of seeing a computation process (speaking in single-thread sense) as something that moves along code-space. At any point a computation knows what it is going to compute and where it is going to continue after it. Capturing that concept of: "where to continue after it" is what I see as a continuation.

A language with support for first class continuations would therefore allow the code to represent those "continuations" and to jump to them. If a person was a "computation" and "code space" was real world, request for a continuation would be asking for your current position and calling a continuation would be teleport there.

Some persons may defined them as some sort of goto, but that is as dynamic goto and not a static one. And due to their power I guess incorrect use may lead to a lot of problems, nonetheless refraining from learning them is like refusing to learn how to ride a bike on a single wheel because with two its safer.

Now I guess my next step is to play with them and see how well I can control this concept. By playing and experiment with them I hope to learn when and when not to use them!

No comments:

Post a Comment