The Little   Funky JavaScripter

Back in the day I was involved as a student in a lab using LISP machines, Prolog and all that jazz to create expert systems for manufacturing mechanical parts. I enjoyed that time a lot, but carried on with my life feeling that those things were far from being applicable in the “real” world.

After a while, I stumbled upon a booklet containing a strange dialogue with lots of round brackets and suddenly realized that I could actually understand it without a hitch. I was spellbound and devoured it within a couple of weeks during my commuting times.

I am still a kind of a practical person, and that bittersweet sensation came back: how can I “use” all this?

Most certainly some eureka! moments, like the discovery of the mind-blowing beauty of multirember&co, got fixed in my deep conscience. Hopefully they still inspire me every time I write some code.

In order to fix my understanding, after a while I decided to port the book code into JavaScript. I started from a few experiments about lists done in precedence. During the work I discovered that I wasn’t the first to have the idea, and that even my hero Douglas Crockford had done it.

Still my implementations is the only one fully functional. When Doug builds a list, he returns a 2-elements JS array [car, cdr], which is a sensible thing to do in JavaScript. But you know it, JS arrays are …mutable |-O=

I went instead for immutable couples defined as functions(*):

function cons(car, cdr) { return function(w) { return w(car, cdr); } }

function car(cons) { return cons(function(car, cdr) { return car; }); }

function cdr(cons) { return cons(function(car, cdr) { return cdr; }); }

That’s why the GitHub project is called The Little Funky JavaScripter.

It contains (almost) everything from the book, even the Y-combinator. Everything is covered with tests. One day I will find the time to complete the last bit, that’s the language interpreter in chapter 10.

(*) Here you can find a longer presentation of the theory behind my functions. The page explains also very clearly why you shouldn’t use this stuff in a real JavaScript app.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s