Ok, continuations is not the way we from the imperative world are use to think. Yet Another Language Geek defines it like this:
CPS (Continuation-Passing Style) turns a program inside-out. In the process, the programmer may feel that his brain has been turned inside-out as well.
He carries on defining some simple samples. Here's his factorial sample:
static void fact(int n, Action<int> action)
{
if (n == 0)
{
action(1);
}
else
{
fact(n - 1, x => action(n * x));
}
}
static void Main(string[] args)
{
fact(5, x => Console.WriteLine(x));
}
Please follow the rest of the his post - it's loaded with samples usable on the real world.
No comments:
Post a Comment