Beispiel #1
0
 public static U FoldR <U>(Func <T, U, U> f, Cons <T> t, U start = default(U))
 {
     return((t == null) ? start : f(t.head, FoldR(f, t.tail)));
 }
Beispiel #2
0
 // In Python, these are not only free functions, but also work on
 // anything that duck-types as a Cons, like LazyCons. In C#, I'd
 // have to create an ICons<T> interface for that. I suppose that's
 // what I'm supposed to do--and an ICons for Generic.ICons<T> to
 // inherit, just like the stuff in .NET. Because what everyone wants
 // is more boilerplate than Java. Anyway, as static members of Cons
 // that work on Cons, these work just fine.
 public static U FoldL <U>(Func <U, T, U> f, Cons <T> t, U start = default(U))
 {
     return((t == null) ? start : FoldL(f, t.tail, f(start, t.head)));
 }
Beispiel #3
0
 public void push(T value)
 {
     lst = new Cons <T>(value, lst);
 }
Beispiel #4
0
        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);
            }
        }
Beispiel #5
0
 public Cons(T head = default(T), Cons <T> tail = null)
 {
     this.head = head;
     this.tail = tail;
 }