diff --git a/js-assembler/runtime-src/baselib-lists.js b/js-assembler/runtime-src/baselib-lists.js index a4ad775..b892267 100644 --- a/js-assembler/runtime-src/baselib-lists.js +++ b/js-assembler/runtime-src/baselib-lists.js @@ -172,7 +172,7 @@ // 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,12 +185,15 @@ // 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; + while (true) { + if (tortoise instanceof Cons) { tortoise = tortoise.rest; } + if (hare instanceof Cons) { hare = hare.rest; } + if (hare instanceof Cons) { hare = hare.rest; } + if (hare === EMPTY) { return true; } + if (tortoise === hare) { return false; } + if ((hare instanceof Cons) === false) { return false; } } return true; }; diff --git a/tests/more-tests/lists.expected b/tests/more-tests/lists.expected index c859ffb..3d52e80 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 diff --git a/tests/more-tests/lists.rkt b/tests/more-tests/lists.rkt index bc49d82..703d4d5 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? '()) 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