Skip to content

Instantly share code, notes, and snippets.

@LCamel
Last active August 7, 2025 21:53
Show Gist options
  • Select an option

  • Save LCamel/1587d565da1e09bf792372e7af32d3be to your computer and use it in GitHub Desktop.

Select an option

Save LCamel/1587d565da1e09bf792372e7af32d3be to your computer and use it in GitHub Desktop.
mutual recursion
var MkIsOdd = (other) => ( (x) => x == 0 ? false : other(x - 1) );
var MkIsEven = (other) => ( (x) => x == 0 ? true : other(x - 1) );
// // doesn't work: isEven is undefined
// var isOdd = MkIsOdd(isEven), isEven = MkIsEven(isOdd);
// // the line above will be translated to something like:
var isOdd = undefined;
var isEven = undefined;
isOdd = MkIsOdd(isEven); // doesn't work: isEven is undefined
isEven = MkIsEven(isOdd);
console.log("OK");
console.log(isOdd(3)); // error
console.log(isEven(3));
var MkIsOdd = (other) => ( (x) => x == 0 ? false : (other())(x - 1) );
var MkIsEven = (other) => ( (x) => x == 0 ? true : (other())(x - 1) );
// var isOdd = MkIsOdd(() => isEven), isEven = MkIsEven(() => isOdd);
var isOdd = undefined;
var isEven = undefined;
isOdd = MkIsOdd(() => isEven); // the value of isEven could be changed later
isEven = MkIsEven(() => isOdd);
console.log("OK");
console.log(isOdd(3));
console.log(isEven(3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment