« My CPAN Site | Main | Lamda Calculus in Perl(5|6) »

Wednesday, 15 March 2006

Lambda Calculus in Javascript

Since (?:ECMA|Java)script supports closures, it is Lambda-calculus ready. I googled examples but there seems none so I made one.

Welcome to the world of λ!

Source:
// define
var zero = function(f){
             return function(x){
               return x}};
var succ = function(n){
             return function(f){
               return function(x){
                 return f(n(f)(x))}}};
var add = function(m){
             return function(n){
               return function(f){
                 return function(x){
                   return m(f)(n(f)(x))}}}};
var mul = function(m){
            return function(n){
              return function(f){
                return m(n(f))}}};
var pow = function(m){
            return function(n){
              return n(m)}};
// execute
function $(id){ return document.getElementById(id) };
var one     = succ(zero);
var two     = succ(one);
var four    = add(two)(two);
var eight   = mul(two)(four);
var sixteen = pow(two)(four);
var numbers = [one, two, four, eight, sixteen];
$('result').innerHTML = '';
for (var i = 0; i < numbers.length; i++){
  var n = numbers[i];
  $('result').innerHTML += numbers[i](function(n){return 1+n})(0);
  $('result').innerHTML += ' = ';
  $('result').innerHTML += numbers[i](function(n){return '(1+' + n + ')'})(0);
  $('result').innerHTML += '<br />';
}
Show me!

 

Also note that this example contains no <script> tag but the "Show Me!" still works. Trick? onclick="eval(document.getElementById('lambda').firstChild.nodeValue)".

Don't you love dynamic lanugages?

Dan the (?:ECMA|Java)scripting Lambacamel

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d8345ac86c69e200d8342620cd53ef

Listed below are links to weblogs that reference Lambda Calculus in Javascript:

» IRS from IRS
IRS [Read More]

Comments

Managed Hosting, Colocation and Data Center Services by victoryushchenkonashpresudent ...

The Iraqi government says guards from US security firm Blackwater killed more people than previously thought.

H!, nice day, look this:
Netvouz is a social bookmark manager where you can store your favorite links online and access them from any computer.

End ^) See you

Hello,
Not sure that this is true) but thanks

Have a nice day
Hobosic

Thank for sharing the great script :-)

thanks for sharing the great script.

Hi, It's the second time i'm posting you without a reply. I found your site using Yaehoo, does your site support firefox?

Hi, It's the second time i'm posting you without a reply. I found your site using Yaehoo, does your site support firefox?

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment