此代码生成它下面的堆栈跟踪:
import R from 'ramda';
function quicksort(list) {
if ( R.isEmpty(list) ) return list;
let pivot = R.head(list);
let lesser = R.filter( e => e < pivot , list );
let greater = R.filter( e => e >= pivot, list );
return R.concat( quicksort(lesser), pivot, quicksort(greater) );
}
var sorted = quicksort([2,3,1]);
console.log(sorted);
堆栈追踪:
$ babel-node quicksort2015.js
/Users/ivan/.nvm/versions/node/v4.1.2/lib/node_modules/babel/node_modules/babel-core/node_modules/core-js/modules/es6.object.to-string.js:3
var classof = require('./$.classof')
^
RangeError: Maximum call stack size exceeded
at Array.toString (native)
at module.exports
(/Users/ivan/.nvm/versions/node/v4.1.2/lib/node_modules/babel/node_modules/babel-core/node_modules/core-js/modules/$.cof.js:4:19)
at module.exports
(/Users/ivan/.nvm/versions/node/v4.1.2/lib/node_modules/babel/node_modules/babel-core/node_modules/core-js/modules/$.classof.js:13:13)
at Array.toString (/Users/ivan/.nvm/versions/node/v4.1.2/lib/node_modules/babel/node_modules/babel-core/node_modules/core-js/modules/es6.object.to-string.js:8:25)
at type (/Users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:3345:100)
at f1 (/Users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:166:27)
at _equals (/Users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:4038:13)
at equals (/Users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:4664:16)
at f2 (/Users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:201:24)
at Object.isEmpty (/Users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:5220:29)
我虽然那个babel会处理尾部调用优化?无论如何,这是一个如此短的清单,它不应该那么深,对吗?
答案 0 :(得分:2)
这可能是由于过滤了整个列表,而不是lesser
和greater
列表的尾部。
e.g。您可以尝试以下内容:
function quicksort(list) {
if (list.length <= 1) return list;
var x = R.head(list);
var xs = R.tail(list);
var lesser = R.filter(R.lt(R.__, x), xs);
var greater = R.filter(R.gte(R.__, x), xs);
return R.concat(quicksort(lesser), R.prepend(x, quicksort(greater)));
}
不幸的是,由于对quicksort
的递归调用不在尾部调用位置,因此尾部调用优化也不会受益。