Lambda returning another lambda

后端 未结 4 1881
自闭症患者
自闭症患者 2021-02-08 15:59

is there any way how to return lambda from another lambda recursively?

All I want to do is finite state machine, implemented as lambda, which returns lambda implementin

4条回答
  •  借酒劲吻你
    2021-02-08 16:27

    Your question is already answered, but those reading this may be interested to note that you can use this technique to embed the Lambda calculus in C#.

    First starting with:

        public delegate Lambda Lambda(Lambda x);
    

    Using various function definitions found at http://en.wikipedia.org/wiki/Lambda_calculus I defined various primitives as follows:

        public static Lambda Id         = x => x;
        public static Lambda Zero       = f => x => x;
        public static Lambda True       = x => y => x;
        public static Lambda False      = x => y => y;
        public static Lambda One        = f => x => f(x);
        public static Lambda Two        = f => x => f(f(x));
        public static Lambda Succ       = n => f => x => f(n(f)(x));
        public static Lambda Three      = Succ(Two);
        public static Lambda Pred       = n => f => x => n(g => h => h(g(f)))(u => x)(Id);
        public static Lambda Plus       = m => n => f => x => m(f)(n(f)(x));
        public static Lambda Sub        = m => n => n (Pred) (m);
        public static Lambda And        = p => q => p(q)(p);
        public static Lambda Or         = p => q => p(p)(q);
        public static Lambda Not        = p => a => b => p(b)(a);
        public static Lambda IfThenElse = p => a => b => p(a)(b);
        public static Lambda IsZero     = n => n(x => False)(True);
        public static Lambda IsLtEqOne  = n => IsZero(Pred(n));
        public static Lambda Pair       = x => y => f => f(x)(y);
        public static Lambda First      = pair => pair(True);
        public static Lambda Second     = pair => pair(False);
        public static Lambda Nil        = x => True;
        public static Lambda Null       = p => p(x => y => False);
        public static Lambda LtEq       = x => y => IsZero(Sub(x)(y));
        public static Lambda Gt         = x => y => LtEq(y)(x);
        public static Lambda Eq         = x => y => And(LtEq(x)(y))(LtEq(y)(x));
        public static Lambda M          = x => x(x);
    

    For various tests and the whole code see: http://code.google.com/p/jigsaw-library/source/browse/trunk/Theory/EmbeddedLambdaCalculus.cs

提交回复
热议问题