JavaScript representations of cons

1) Efficient implementation, but doesn’t allow for improper lists:

['a', 'b', 'c']

car = function(xs) { return xs[0]; }

cdr = function(xs) { return xs.slice(1); }

2) Less efficient, but full control over the cons pair:

['a', ['b', ['c', null]]

car = function(xs) { return xs[0]; }

cdr = function(xs) { return xs[1]; }

3)┬áSimilar to 2 – not sure what the advantages are, if any:

{car: ‘a’, cdr: {car: ‘b’, cdr: {car: ‘c’, cdr: null}}}

car = function(xs) { return xs.car; }

cdr = function(xs) { return xs.cdr; }

 

About Evan

Works on a sight reading practice tool for musicians and music teachers, and loves making Try Arc better in his spare time.

, ,

No comments yet.

Leave a Reply