diff --git a/js-assembler/runtime-src/baselib-lists.js b/js-assembler/runtime-src/baselib-lists.js index a4ad775..4df1e7e 100644 --- a/js-assembler/runtime-src/baselib-lists.js +++ b/js-assembler/runtime-src/baselib-lists.js @@ -48,19 +48,20 @@ this.rest = rest; }; + var makePair = function (first, rest) { + return new Cons(first, rest); + }; + Cons.prototype.reverse = function () { var lst = this; var ret = EMPTY; while (lst !== EMPTY) { - ret = Cons.makeInstance(lst.first, ret); + ret = makePair(lst.first, ret); lst = lst.rest; } return ret; }; - Cons.makeInstance = function (first, rest) { - return new Cons(first, rest); - }; // FIXME: can we reduce the recursion on this? Cons.prototype.equals = function (other, aUnionFind) { @@ -82,7 +83,7 @@ var ret = b; var lst = this.reverse(); while (lst !== EMPTY) { - ret = Cons.makeInstance(lst.first, ret); + ret = makePair(lst.first, ret); lst = lst.rest; } @@ -160,19 +161,18 @@ var isEmpty = function (x) { return x === EMPTY; }; - var makePair = Cons.makeInstance; var makeList = function () { var result = EMPTY, i; for (i = arguments.length - 1; i >= 0; i--) { - result = Cons.makeInstance(arguments[i], result); + result = makePair(arguments[i], result); } return result; }; // Coerse a list back into a JavaScript array. - var listToArray = function(lst) { + var listToArray = function (lst) { var result = []; while (lst !== EMPTY) { result.push(lst.first); @@ -185,14 +185,17 @@ // isList: Any -> Boolean // Returns true if x is a list (a chain of pairs terminated by EMPTY). var isList = function (x) { - while (x !== EMPTY) { - if (x instanceof Cons) { - x = x.rest; - } else { - return false; - } + var tortoise, hare; + tortoise = hare = x; + if (hare === EMPTY) { return true; } + while (true) { + if (!(hare instanceof Cons)) { return false; } + if (tortoise instanceof Cons) { tortoise = tortoise.rest; } + hare = hare.rest; + if (hare instanceof Cons) { hare = hare.rest; } + if (hare === EMPTY) { return true; } + if (tortoise === hare) { return false; } } - return true; }; diff --git a/js-assembler/runtime-src/baselib-vectors.js b/js-assembler/runtime-src/baselib-vectors.js index b7daaa9..b3cc0ea 100644 --- a/js-assembler/runtime-src/baselib-vectors.js +++ b/js-assembler/runtime-src/baselib-vectors.js @@ -59,7 +59,7 @@ Vector.prototype.toList = function () { var ret = baselib.lists.EMPTY, i; for (i = this.length() - 1; i >= 0; i--) { - ret = baselib.lists.Cons.makeInstance(this.elts[i], ret); + ret = baselib.lists.makePair(this.elts[i], ret); } return ret; }; diff --git a/tests/more-tests/lists.expected b/tests/more-tests/lists.expected index c859ffb..296e538 100644 --- a/tests/more-tests/lists.expected +++ b/tests/more-tests/lists.expected @@ -2,6 +2,10 @@ ("hello" "world") true true +true +false +true +true false true hello @@ -19,3 +23,4 @@ true false false true +(3 1 4) diff --git a/tests/more-tests/lists.rkt b/tests/more-tests/lists.rkt index bc49d82..fc56698 100644 --- a/tests/more-tests/lists.rkt +++ b/tests/more-tests/lists.rkt @@ -3,6 +3,11 @@ '(1 2 3) (list "hello" "world") +(list? empty) +(list? '(1)) +(list? '(1 2)) +(list? 1) + (empty? empty) (empty? '()) (cons? '()) @@ -23,4 +28,7 @@ (ormap even? '(2 4 5 8)) (ormap even? '(5)) (ormap even? '(1 3 5 7)) -(ormap even? '(1 3 8 7)) \ No newline at end of file +(ormap even? '(1 3 8 7)) + + +(vector->list #(3 1 4)) diff --git a/tests/more-tests/sharing.expected b/tests/more-tests/sharing.expected index 8b7c869..bd6f4c7 100644 --- a/tests/more-tests/sharing.expected +++ b/tests/more-tests/sharing.expected @@ -22,3 +22,5 @@ jill jane +true +false diff --git a/tests/more-tests/sharing.rkt b/tests/more-tests/sharing.rkt index 55ebb7a..3a95aa6 100644 --- a/tests/more-tests/sharing.rkt +++ b/tests/more-tests/sharing.rkt @@ -70,4 +70,12 @@ (for-each displayln (map person-name (person-friends b))) (newline) (for-each displayln (map person-name (person-friends c))) - (newline)) \ No newline at end of file + (newline)) + + + +;; Make sure cyclic lists are treated correctly by list? +(shared ([a (cons 1 a)]) + (begin + (displayln (pair? a)) + (displayln (list? a)))) \ No newline at end of file