Last active
July 11, 2023 13:30
-
-
Save lgastako/e7625f9317d11612b64bf3e3919fca46 to your computer and use it in GitHub Desktop.
Finally figured out how to write fizzbuzz in python
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
ADD = lambda a: lambda b: \ | |
lambda f: lambda x: b(f)(a(f)(x)) | |
MULT = lambda a: lambda b: \ | |
lambda f: lambda x: a(b(f))(x) | |
ZERO = lambda f: lambda x: x | |
ONE = lambda f: lambda x: f(x) | |
TWO = ADD(ONE)(ONE) | |
THREE = ADD(ONE)(TWO) | |
FOUR = ADD(TWO)(TWO) | |
FIVE = ADD(TWO)(THREE) | |
FIFTEEN = MULT(THREE)(FIVE) | |
TEN = MULT(TWO)(FIVE) | |
HUNDRED = MULT(TEN)(TEN) | |
INC = ADD(ONE) | |
DEC = lambda n: \ | |
lambda f: \ | |
lambda x: n(lambda g: lambda h: h(g(f))) \ | |
(lambda y: x)(lambda y: y) | |
SUB = lambda m: lambda n: n(DEC)(m) | |
TRUE = lambda t: lambda f: t | |
FALSE = lambda t: lambda f: f | |
IF = lambda b: b | |
IS_ZERO = lambda n: n(lambda _: FALSE)(TRUE) | |
IS_LTE = lambda m: lambda n: IS_ZERO(SUB(m)(n)) | |
Z = lambda f: (lambda x: f(lambda y: x(x)(y) ) ) (lambda x: f(lambda y: x(x)(y)) ) | |
MOD = Z(lambda f: lambda m: lambda n: \ | |
IF (IS_LTE(n)(m)) | |
(lambda x: f(SUB(m)(n))(n)(x)) | |
(m)) | |
PAIR = lambda x: lambda y: lambda f: f(x)(y) | |
LEFT = lambda p: p(TRUE) | |
RIGHT = lambda p: p(FALSE) | |
NIL = PAIR(TRUE)(TRUE) | |
IS_EMPTY = LEFT | |
FIRST = lambda xs: LEFT(RIGHT(xs)) | |
REST = lambda xs: RIGHT(RIGHT(xs)) | |
CONS = lambda xs: lambda x: PAIR(FALSE)(PAIR(x)(xs)) | |
RANGE = Z(lambda f: lambda m: lambda n: \ | |
IF (IS_LTE(m)(n)) ( | |
lambda x: CONS(f(INC(m))(n))(m)(x) | |
)(NIL)) | |
FOLD = Z(lambda f: lambda xs: lambda x: lambda g: \ | |
IF (IS_EMPTY(xs)) | |
(x) | |
(lambda y: | |
g (f(REST(xs))(x)(g)) (FIRST(xs)) (y) | |
)) | |
MAP = lambda f: lambda k: FOLD(k)(NIL)( | |
lambda xs: lambda x: CONS(xs)(f(x)) | |
) | |
FLIP = lambda f: lambda a: lambda b: f(b)(a) | |
FOR = FLIP(MAP) | |
B = TEN | |
F = INC(B) | |
I = INC(F) | |
U = INC(I) | |
ZED = INC(U) | |
CONCAT = lambda a: lambda b: FOLD(a)(b)(CONS) | |
FIZZ = CONS(CONS(CONS(CONS(NIL)(ZED))(ZED))(I))(F) | |
BUZZ = CONS(CONS(CONS(CONS(NIL)(ZED))(ZED))(U))(B) | |
FIZZBUZZ = CONCAT(FIZZ)(BUZZ) | |
DIV = Z(lambda f: lambda m: lambda n: | |
IF (IS_LTE(n)(m)) (lambda x: INC(f(SUB(m)(n))(n))(x))(ZERO)) | |
SNOC = lambda xs: lambda x: FOLD(xs)(CONS(NIL)(x))(CONS) | |
TO_DIGITS = Z(lambda f: lambda n: SNOC( | |
IF (IS_LTE (n) (DEC(TEN))) | |
(NIL) | |
(lambda x: f(DIV(n)(TEN))(x)) | |
) | |
(MOD(n)(TEN))) | |
fb = (FOR (RANGE (ONE) (HUNDRED)) | |
(lambda n: | |
IF (IS_ZERO(MOD (n) (FIFTEEN))) | |
(FIZZBUZZ) | |
(IF (IS_ZERO(MOD (n) (THREE))) | |
(FIZZ) | |
(IF (IS_ZERO(MOD (n) (FIVE))) | |
(BUZZ) | |
(TO_DIGITS(n)))))) | |
def main(): | |
list(map(print, to_str_list(fb))) | |
def to_digits(N): | |
return to_str(TO_DIGITS(N)) | |
def to_char(C): | |
return "0123456789BFiuz"[to_int(C)] | |
def to_str(S): | |
return "".join(to_list_with(to_char)(S)) | |
def to_list(XS): | |
result = [] | |
while not to_bool(IS_EMPTY(XS)): | |
result.append(FIRST(XS)) | |
XS = REST(XS) | |
return result | |
def to_list_with(f): | |
return lambda XS: list(map(f, to_list(XS))) | |
def to_bool(B): | |
return B(True)(False) | |
def to_int(N): | |
return N(lambda x: x + 1)(0) | |
to_int_list = to_list_with(to_int) | |
to_str_list = to_list_with(to_str) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment