Can someone explain that code to me?

Asked

Viewed 79 times

0

There is this following code and I’m very confused about it, even fiddling with javascript, I tried debugging but I put a console.log inside the function in the variable "T" and it printa truth, false... but no parameter is passed to this T, in reality it happens in reverse pass the T within the AND, at last I am quite confused about it. I just need to understand why to perform from the display to the AND etc...

        
        var T = function (a, b) {
        console.log(a,b);
            return a;
        };
        
        var F = function (a, b) {
            return b;
        };
        
        var display = function (b) {
        		//console.log(b);
            return b('verdadeiro', 'falso');
        };
        
        var NOT = function (x) {
            return x(F, T);
        };
        
        var AND = function (a, b) {
            return a(b, F);
        };
        
        var OR = function (a, b) {
            return a(t, B);
        };
        
        // agora testando:
        alert(display(AND(T, T))); // Vai exibir Verdadeiro, como deveria.

1 answer

4


You pass T for the function AND:

AND(T, T)

And she invokes T passing by b (in this case also equal to T) and F:

var AND = function (a, b) {
    return a(b, F);
};

The execution logic of this whole code is more or less like this:

  1. AND(T, T) returns T(T, F)
  2. T(T, F) returns T
  3. display receives T and returns T('verdadeiro', 'falso')
  4. T('verdadeiro', 'falso') returns 'verdadeiro'
  • but T takes 2 parameters and when it returns Return a(b, F); Both F and b(which in this case is another T) receive 2 parameters, and only call them there

  • I put a supplement in the answer, see if it clarifies you.

  • Ah ta I understand now, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.