This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// file: tail-call-babel.js | |
'use strict'; | |
// #1 "naive" | |
function factorial1(x) { | |
if (x <= 0) { | |
return 1; | |
} else { | |
return x * factorial1(x - 1); | |
} | |
} | |
// #2 tail rec using a default parameter | |
function factorial2(n) { | |
return facRec2(n); | |
} | |
function facRec2(_x2) { | |
var _arguments = arguments; | |
var _again = true; | |
_function: while (_again) { | |
var x = _x2; | |
_again = false; | |
var acc = _arguments.length <= 1 || _arguments[1] === undefined ? 1 : _arguments[1]; | |
if (x <= 1) { | |
return acc; | |
} else { | |
_arguments = [_x2 = x - 1, x * acc]; | |
_again = true; | |
acc = undefined; | |
continue _function; | |
} | |
} | |
} | |
// #3 tail rec | |
function factorial3(n) { | |
return facRec3(n, 1); | |
} | |
function facRec3(_x3, _x4) { | |
var _again2 = true; | |
_function2: while (_again2) { | |
var x = _x3, | |
acc = _x4; | |
_again2 = false; | |
if (x <= 1) { | |
return acc; | |
} else { | |
_x3 = x - 1; | |
_x4 = x * acc; | |
_again2 = true; | |
continue _function2; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment