static public LazyCons <T> FromEnumerable(IEnumerator <T> e) { if (!e.MoveNext()) { return(null); } return(new LazyCons <T>(e.Current, () => LazyCons <T> .FromEnumerable(e))); }
static void Main(string[] args) { var xs = Cons <int> .FromEnumerable(new int[] { 1, 2, 3 }); foreach (var x in xs) { Console.WriteLine(x); } var ys = xs.tail; xs.tail.head *= -1; foreach (var y in ys) { Console.WriteLine(y); } var zs = LazyCons <int> .FromEnumerable(xs); foreach (var z in zs) { Console.WriteLine(z); } foreach (var z in zs) { Console.WriteLine(z); } Func <int, Cons <int>, Cons <int> > cons = (q, qs) => new Cons <int>(q, qs); var ws = Cons <int> .FoldR(cons, xs); foreach (var w in ws) { Console.WriteLine(w); } var vs = Cons <int> .FoldL(Funky.Flip(cons), xs); foreach (var v in vs) { Console.WriteLine(v); } var ss = new Stack <int>(); ss.push(1); ss.push(2); Console.WriteLine(ss.pop()); Console.WriteLine(ss.pop()); Console.WriteLine(ss.pop(0)); try { Console.WriteLine(ss.pop()); } catch (Exception e) { Console.WriteLine(e); } var ms = new MStack <int>(); ms.push(1); ms.push(2); Console.WriteLine(ms.pop()); Console.WriteLine(ms.pop()); Console.WriteLine(ms.pop(0)); try { Console.WriteLine(ms.pop()); } catch (Exception e) { Console.WriteLine(e); } }