diff --git a/examples/list-length.rkt b/examples/list-length.rkt index 81e7044..bb6899b 100644 --- a/examples/list-length.rkt +++ b/examples/list-length.rkt @@ -1,11 +1,13 @@ #lang planet dyoo/whalesong -(define (mylen x) +(define (mylen x acc) (cond [(empty? x) - 0] + acc] [else - (add1 (mylen (rest x)))])) + (mylen (rest x) (add1 acc))])) "computing length" -(mylen (build-list 100000 (lambda (i) i))) +(define v (build-list 1000000 (lambda (i) i))) +(printf "Built list\n") +(mylen v 0) "done computing length" \ No newline at end of file diff --git a/js-assembler/runtime-src/baselib-lists.js b/js-assembler/runtime-src/baselib-lists.js index b003bf6..28a20bd 100644 --- a/js-assembler/runtime-src/baselib-lists.js +++ b/js-assembler/runtime-src/baselib-lists.js @@ -264,18 +264,26 @@ var tortoise, hare; tortoise = hare = x; if (hare === EMPTY) { - tortoise._isList = true; return true; } while (true) { if (!(hare instanceof Cons)) { return false; } if (tortoise instanceof Cons) { - if (tortoise._isList === true) { return true; } tortoise = tortoise.rest; } hare = hare.rest; - if (hare instanceof Cons) { hare = hare.rest; } - if (hare === EMPTY) { return true; } + if (hare instanceof Cons) { + // optimization to get amortized linear time isList: + if (hare._isList) { tortoise._isList = true; return true; } + hare = hare.rest; + // optimization to get amortized linear time isList: + if (hare instanceof Cons && hare._isList) { tortoise._isList = true; return true; } + } + if (hare === EMPTY) { + // optimization to get amortized linear time isList: + tortoise._isList = true; + return true; + } if (tortoise === hare) { return false; } } }; diff --git a/js-assembler/runtime-src/baselib-vectors.js b/js-assembler/runtime-src/baselib-vectors.js index c9d600d..1bb72b6 100644 --- a/js-assembler/runtime-src/baselib-vectors.js +++ b/js-assembler/runtime-src/baselib-vectors.js @@ -15,10 +15,6 @@ for (i = 0; i < n; i++) { this.elts[i] = initialElements[i]; } - } else { - for (i = 0; i < n; i++) { - this.elts[i] = undefined; - } } this.mutable = true; }; diff --git a/js-assembler/runtime-src/runtime.js b/js-assembler/runtime-src/runtime.js index 655255a..1f50cd0 100644 --- a/js-assembler/runtime-src/runtime.js +++ b/js-assembler/runtime-src/runtime.js @@ -395,7 +395,8 @@ var frame = this.c[this.c.length - 1]; var marks = frame.marks; var i; - for (i = 0; i < marks.length; i++) { + var l = marks.length; + for (i = 0; i < l; i++) { if (key === marks[i][0]) { marks[i][1] = value; return;