AjLisp in Javascript (Part 3) Define, Lambda and Closures

Previous Post

Lets review the new forms definition in AjLisp, my Lisp interpreter written in Javascript (github repository). A key special form in AjLispJs, is define form:

var defineForm = new SpecialForm();
defineForm.eval = function eval(list, env)
	var name = list.first().name();
	var value = list.rest().first();
	var body = list.rest().rest();
	if (isNil(body)) {
		value = evaluate(value, env);
	else {
		value = new Closure(value, env, body);
	environment[name] = value;
	return value;

You can use to define simple global data. My initial tests:

test("Evaluate Simple Expression with Atoms", function() {
	eval("(define one 1)");
	eval("(define two 2)");
	eval("(define three 3)");
	equal(eval("one"), 1);
	equal(eval("(quote one)").asString(), "one");
	equal(eval("(list one two three)").asString(), "(1 2 3)");
	equal(eval("(first (list one two three))"), 1);
	equal(eval("(rest (list one two three))").asString(), "(2 3)");
	equal(eval("(cons one (list two three))").asString(), "(1 2 3)");

But define is prepared to accept three parameters:

(define mapfirst (fn lst)
  (if (nilp lst)
      (fn (first lst))
      (mapfirst fn (rest lst))

The first parameter is the name of the global environment property. The second is a list of parameters. The third is the form to apply. Define is used to define new functions. It’s equivalent to

(define mapfirst (lambda (fn lst)
    (if (nilp lst)
        (fn (first lst))
        (mapfirst fn (rest lst))

Note the lambda. It is an special form:

var lambdaForm = new SpecialForm();
lambdaForm.eval = function eval(list, env)
    var argnames = list.first();
    var body = list.rest();
    return new Closure(argnames, env, body);

Both define (with three parameters) and lambda create a Closure. The closure receives a list of argument names, a closure environment and a body. Then, when a closure is evaluated:

function Closure(argnames, closureenv, body) {
	body = new List(doForm, body);
	this.eval = function(args, environment) {
		var newenv = makeEnvironment(argnames, args, closureenv);
		return evaluate(body, newenv);
Closure.prototype.evaluate = Form.prototype.evaluate;
Closure.prototype.apply = Form.prototype.apply;

The key part is the .eval: closure evaluation employs a new environment, based not in the current environment, but in the closureenv, the environment received in the constructor when the closure was created. This is a typical implementation in Lisp interpreter.

Next topics to discuss in upcoming posts: flambda, mlambda, macro evaluation, parser.

Keep tuned!

Angel “Java” Lopez



One thought on “AjLisp in Javascript (Part 3) Define, Lambda and Closures

  1. Pingback: AjLisp in Javascript (Part 2) List Evaluation, Forms and Special Forms « Angel “Java” Lopez on Blog

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s