Last active
January 7, 2024 15:14
-
-
Save FrankFang/f946fce5ab8b33319e3d600b44d775b3 to your computer and use it in GitHub Desktop.
λ演算
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
ZERO = f => x => x | |
ONE = f => x => f(x) | |
TWO = f => x => f(f(x)) | |
THREE = f => x => f(f(f(x))) | |
FOUR = f => x => f(f(f(f(x)))) | |
FIVE = f => x => f(f(f(f(f(x))))) | |
SIX = f => x => f(f(f(f(f(f(x)))))) | |
SEVEN = f => x => f(f(f(f(f(f(f(x))))))) | |
EIGHT = f => x => f(f(f(f(f(f(f(f(x)))))))) | |
NINE = f => x => f(f(f(f(f(f(f(f(f(x))))))))) | |
TEN = f => x => f(f(f(f(f(f(f(f(f(f(x)))))))))) | |
TRUE = x => y => x // 选第一个 | |
FALSE = x => y => y // 选第二个 | |
IF = bool => value1 => value2 => bool(value1)(value2) | |
// IF = bool => bool | |
IS_ZERO = n => n(() => FALSE)(TRUE) | |
UP = n => f => x => f(n(f)(x)) | |
PAIR = x => y => f => f(x)(y) | |
FIRST = p => p(x => y => x) | |
SECOND = p => p(x => y => y) | |
SLIDE = p => PAIR(SECOND(p))(UP(SECOND(p))) | |
DOWN = n => FIRST( n(SLIDE)(PAIR(ZERO)(ZERO)) ) | |
ADD = m => n => n(UP)(m) // m + n = m +1 +1 +1 | |
SUB = m => n => n(DOWN)(m) // m - n = m -1 -1 -1 | |
MUL = m => n => n(ADD(m))(ZERO) // m * n = 0 +m +m +m | |
POW = m => n => n(MUL(m))(ONE) // m ** n = 1 *m *m *m | |
// ------------------------------ | |
toNumber = n => n(x => x + 1)(0) | |
toScratches = n => n(x => x + "|")("") | |
toBool = b => b(true)(false) | |
// p = PAIR(FOUR)(TWO) // [4, 2] | |
// f = FIRST(p) | |
// s = SECOND(p) | |
// console.log(toNumber(DOWN(TEN))) | |
// console.log(toNumber(f)) | |
// console.log(toNumber(s)) | |
// console.log(toBool(IS_ZERO(ZERO))) // true | |
// console.log(toBool(IS_ZERO(ONE))) // false | |
// console.log(toBool(IS_ZERO(TEN))) // false | |
// console.log(IF(TRUE)("yes")("no")) // yes | |
// console.log(IF(FALSE)("yes")("no")) // no | |
// console.log(toBool(TRUE)) // true | |
// console.log(toBool(FALSE)) // false | |
// console.log(toNumber(ONE)) // 1 | |
// console.log(toNumber(ZERO)) // 0 | |
// console.log(toNumber(TEN)) // 10 | |
// console.log(toScratches(ONE)) // 1 | |
// console.log(toScratches(ZERO)) // 0 | |
// console.log(toScratches(TEN)) // 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment