I wan't to play around with tail call optimization in node/es2015, but I keep getting
RangeError: Maximum call stack size exceeded
function countTo(n, acc) {
if(n === 0) {
return acc;
}
return countTo(n - 1, acc + n);
}
console.log(countTo(100000 , 0))
'use strict';
--harmony
--harmony-tailcalls
#lang racket
(define count-to
(lambda (n acc)
(cond
((= n 0) acc)
(else (count-to (- n 1) (+ acc n))))))
(count-to 100000000 0)
; ~> 5000000050000000
"use strict";
--harmony
--harmony-tailcalls
Using node v6.5.0, the following works :
function countTo(n, acc) {
'use strict';
if(n === 0) {
return acc;
}
return countTo(n - 1, acc + n);
}
console.log(countTo(100000 , 0));
Running with --harmony-tailcalls
flag :
node --harmony-tailcalls tco.js