Implement Lambda Expression TRUE, FALSE, AND, OR and NOT with Javascript

Asked

Viewed 910 times

3

I was reading about lambda in that article and showed examples of functional programming implementing TRUE, FALSE, NOT, AND and OR with Ruby:

T = lambda { |a,b| a } 
F = lambda { |a,b| b } 
display = lambda { |boolean| boolean['verdadeiro','falso']}

NOT = lambda{ |x| x[F,T] }
AND = lambda{ |a,b| a[b,F] }
OR = lambda{ |a,b| a[T,b] }

Implementing the same examples with Javascript?

1 answer

7


You chose a rather complex article to start understanding the lambas, huh? : ) It could have started with a simpler.

First, you need to understand what a lambda is: it is an anonymous function, which has not assigned it a specific name. This sounds different for those who are accustomed to always create functions giving their name, for example (in Javascript):

function minha_funcao() {}

So how do you create a function without giving it a name? Both Javascript and Ruby allow this (by the way, many other languages allow, even Java now in version 8 is allowing).

var funcao = function(param) {}

Note that the above function does not have a name. In fact, it was assigned to a variable and the variable name, tempting as it may be, is not the name of the function, but a reference to that function. So, how is this useful?

var funcao1 = function(param) {
     alert(param);
}

function funcao2() {
    funcao1("Testando");
}

You can pass functions as a parameter to other functions! And then things get sinister and interesting! In the example above, an Alert will be displayed with the string "Testing".

In Ruby, this same task is done differently, using the keyword lambda, but I’d rather not explain too much about how to do this in Ruby, since your intention is to learn how to do it in Javascript. Now, let’s do the examples you passed in Ruby but in JS:

var T = function(a,b) { return a; };
var F = function(a,b) { return b; };
var display = function(booleano) { return booleano('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,F))); // Vai exibir verdadeiro, como deveria.

I created this fiddle with this example: http://jsfiddle.net/marloncarvalho/jttr71hu/. This example you brought is a little complicated to understand, but it is interesting because it makes use of lambda functions masterfully.

Note that the function saved in the variable T takes two parameters (two lambda functions too) and returns only the first function. The function in F returns the second parameter. From then on, it defines how NOT, AND and OR would be using only these functions!

If you use jQuery, you know the method exists each (https://api.jquery.com/each/) that takes a function as parameter. This function you pass is a lambda!

  • Perfect! Finally I managed to understand =D thank you very much!

  • 1

    Great answer. I take this opportunity to say that in the next version of Javascript (Ecmascript 6) has a shorter syntax for anonymous functions, called "Arrow functions", and that already supported in FF and Chrome Canary. For example, you can do it let T = (a,b) => a;

  • @bfavaretto, I was reading about this new syntax in version 6, very interesting!

Browser other questions tagged

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