ChimeraCoder / MLang

Other
4 stars 0 forks source link

Check that cons/car/cdr are implemented properly (and homoiconically) #5

Open ChimeraCoder opened 12 years ago

ChimeraCoder commented 12 years ago

Lists of arbitrary length shouldn't (technically) be implemented natively. Really, all we have are cons cells, which look like the following:

(x . y)

and are created with the cons function

(cons x y)
(x . y) 

All that car and cdr do are access the first and second elements of a cons cell, respectively. In this case, those would be x and y. However, the second element can be a reference to another cons cell, which means that we get

(cons x (cons y z)) or (x . (y . z)) or (x y z)

which are all equivalent.

The fact that there are no lists of arbitrary length - just cons-cells of length two, which may happen to be chained together to simulate lists, actually helps make other functions simpler to implement (because you never have to worry about how to handle variable length. All you have to do is write the base case (cell has two literal elements) and the recursive case (cell has a literal first element and the second element points to another cell), and everything will Lisp-magically take care of itself).

Let's make sure we're consistent with these everywhere, because that will help us make this much simpler to implement than if we're inconsistent with our implementation.

na-na commented 12 years ago

tested cons car and cdr.. working

sarda-nikhil commented 12 years ago

Closing this issue.

ChimeraCoder commented 12 years ago

Reopening for the same reason that I'm reopening the others - a clean checkout of testing still doesn't run the tests properly, so I can't test that they're working homoiconically (beyond simply working properly).