Lambda Calculas

        

Scenario:

  • λx.x [Function Notation,Argument.Body(Return)] like function (x) {return x}
  • Function Application - λx.x (y). So substitute all x with y
  • λx(bound variable).x y(free variable). 
    • Substitution/Reduction
      • Substitution is left-applicative as you apply from left (when value is applied to a function, it is substituted in the function itself)
      • E.g. - λx.x5 => x[x: = 5] => 5
      •  λx. λy.y x => λx. λy.y => λy.y
    • Multiple Variables
      • λx.λy.t also called currying, so its like Function x has body of λy.t [Function calling Function]
    • Parenthesis:
      • λx.(λy.y x) => λx.(λy.yx) => λx.(y[y: = x]) => λx.x
      • λx.λy.y x => λx.λy.yx => λx.(λy.y) => λy.y
    • Combinators [Combination of Functions]
      • Boolean
        • λx.λy.x [true]
        • λx.λy.y [false] 
      • Conditional
        • λx.λy.λz.x y z
      • Numbers
        • λf.λx.x=0
        • λf.λx.f(x)=1 => λx.x (like identity)
        • λf.λx.f(f(x))=2
        • λf.λx.f(f(f(x)))=3
      • Omega Combinator [looping]
        • Ω => λx.xx(λx.xx) => λx.xxλx.xx
      • Y combinator [for each, only once]:
        • Y = λf.(λx.f(xx))f.(λx.f(xx))
    //Identity Function/I Combinator
    
    function myFunc(x){return x}
    
    console.log((x=>x)(10));
    
    
    //Constant Function
    
    let y = 5
    
    console.log((x=>y)(10));
    
    
    //Substitution
    
    //Constant [Returns 5]
    let y = 5
    
    console.log((x=>y)(10));
    
    //Identity [Returns 10]
    let y = 5
    
    console.log((x=>x)(10));
    
    
    //Multiple
    
    console.log(((x,y) => x+y)(5,10));
    
    
    //5
    console.log((x => y => x + y)(2)(3));
    
    //Error
    console.log((x => x) (y => y)(2)(3));
    
    //2
    console.log((x => x) (y => y)(2));
    
    
    //Combinators 
    
    //Boolean
    
    let True = (x => y => x);
    
    let False = (x => y => y);
    
    console.log(True(true)(false));
    
    console.log(False(true)(false));
    
    let If = (x => y => z => x(y)(z));
    
    console.log(If(True)("TRUE")("Yes"));
    
    console.log(If(False)("NOT")("nah"));
    
    //Numbers:
    
    let calculate = f => f(x => x + 1) (0)
    
    let zero = f => x => x;
    let one = f => x => f(x);
    let two = f => x => f(f(x));
    
    console.log(calculate(zero));
    console.log(calculate(one));
    console.log(calculate(two));
    
    
    //Omega [for]
    let Omega = x => x(x);
    console.log(Omega(Omega));
    
    //Y Combinator [For each]
    //Fibonnaci
    let Y = f => (x => x(x))(x => f(y => x(x)(y)));
    let fib = f => n => n <=1 ? n : f(n-1) + f(n-2);
    let fibfunc = Y(fib);
    console.log(fibfunc(10));

1 comment:

  1. Great blog and wonderful content,
    We are much interested to read this valuable content and keep posting the blogs
    DOT NET Online Training in Hyderabad

    ReplyDelete

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...