lists in JS

22 views
Skip to first unread message

Mark Tarver

unread,
May 31, 2015, 6:53:18 AM5/31/15
to qil...@googlegroups.com
I understand you have lists in JS

var list = ["a",  "b",  "c"]

What are the equivalent of CONS CAR CDR?

Mark

Ramil Farkhshatov

unread,
May 31, 2015, 7:29:57 AM5/31/15
to dr.mt...@gmail.com, qil...@googlegroups.com
Mark Tarver <dr.mt...@gmail.com> wrote:

> I understand you have lists in JS
>
> *var list = ["a", "b", "c"]*
>
> What are the equivalent of CONS CAR CDR?

Actually it is an array, not a list. Javascript doesn't have linked
lists as a built-in type as far as I know. However it is easy to
implement:

function cons(head, tail) {
return {head: head, tail: tail};
}

function car(x) {
return x.head;
}

function cdr(x) {
return x.tail;
}

Also Shen-js has

var list = shen.Cons("a", shen.Cons("b", shen.Cons("c", [])));
// or
var list = shen.list(["a", "b", "c"]);

// and accessors
var list_car = list.head;
var list_cdr = list.tail;
Reply all
Reply to author
Forward
0 new messages