Skip to content

Instantly share code, notes, and snippets.

@Porges
Last active December 19, 2015 14:59
Show Gist options
  • Save Porges/5972846 to your computer and use it in GitHub Desktop.
Save Porges/5972846 to your computer and use it in GitHub Desktop.
I call it... stack-continuation programming!
using System;
static class Program
{
internal delegate D D(D d);
static void Main(string[] args)
{
for (var d = Count(10, (Print("hello")))(Print("done"))(Done); ;)
{
d = d(d);
}
}
static D Done(D d)
{
Environment.Exit(0);
return Done;
}
static D Count(int i, D d)
{
return _ => i == 0 ? _ : d(Count(i - 1, d))(_);
}
static D Print(string something)
{
return _ =>
{
Console.WriteLine(something);
return _;
};
}
static D Repeat(D d)
{
return d;
}
}
type D = delegate of D -> D
let exit = new D(fun d ->
System.Environment.Exit 0
d)
let rec count i d = fun dd ->
match i with
| 0 -> dd
| _ -> d (count (i-1) d) dd
let print something = fun dd -> printfn something; dd
let rec eval (d:D) = eval(d.Invoke(d))
[<EntryPoint>]
let main argv =
eval( count 10 (print "hello") (print "done") exit )
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment