Again you can use the CAR and CDR properties to walk the tree and access elements.

For example to display the A and B items you would use:

console.log(tree.CAR.CAR.CAR);console.log(tree.CAR.CDR.CAR);

and to display the other half of the tree:

console.log(tree.CDR.CAR.CAR);console.log(tree.CDR.CDR.CAR);

You should have the idea well enough now to create and use a tree of any size.

In practice you wouldn't do it manually in this way but instead create some functions that allow you to build the tree without quite so many brackets getting in the way.

Tree walk

Of course if you try the display function on the tree it doesn't work because it assumes that a CAR is always going to be a data item to display rather than perhaps another list. That is the function assumes that CAR will always be a data item and CDR another list.

To convert it to work with any sort of list we have to deal with the possibility that CAR and CDR might be a list.

The function starts in the same way:

function displayList(L){ if(L===null) return;

If the list isn't null then we need to display its CAR but only if it isn't a list that needs further processing.

So how can you tell if L.CAR is a list?

Simple, if it is a list then it too has a CAR and CDR property.

So we can test using:

if (L.CAR.CAR === undefined) {

and if it is undefined then we can assume that L.CAR is a data item and we display it:

console.log(L.CAR);

If it isn't a data item then it is a list and we need to process it further so the else part of the if statement is:

}else{ displayList(L.CAR);}

Finally when the if statement is complete all of the tree that is accessible following the CAR side of the first node has been displayed i.e. the left part of the tree.

Then you will see A,B,C,D printed as the terminal nodes of the tree are encountered left-to-right.

Notice that now that the display function treats both the CAR and CDR parts of a node as possible list the function can deal with lists of any complexity.

Where next

So where to from here?

You can work your way though the Lisp set of functions if you really want, to implementing each one as you go, but you still won't have a Lisp implementation. Lisp is more than just list handling but not much more. What is amazing is that so much can be achieved using so little. Back in the early days when Lisp was first invented it really didn't amount to much more than CONS, CAR and CDR functions and it captured the minds of its users because it was so simple and yet so powerful.

JavaScript has many of the facilities of Lisp and if you want to you can write Lisp-style functional programs using it, but is is just one aspect of this chameleon of a language.

In most cases it is better to try to understand JavaScript on its own terms and try not to make it look like another language.

JavaScript is a subtle and sophisticated language that deserves to be treated in its own right and not as a poor copy of other object-oriented language. In this chapter (Chapter 8) Ian Elliot explores [ ... ]

What is the fuss about strong typing really all about? JavaScript doesn't make much use of type so what is it missing? What is more difficult to do in JavaScript than in a typed language? Are there th [ ... ]