■ソース
//リストの平方和を求める関数sumOfSquares function accumulate(combiner, nullValue, l){ if(l.length == 0){ return nullValue; } var first = l.shift(); return combiner( first, accumulate( combiner, nullValue, l ) ); } function sumOfSquares(lst){ return accumulate( function(x,y){return x*x+y}, 0, lst ); } //sumOfSquares([1,2,3]) //14
■コメント
うーん。。。しゅごい!!!