Last active
August 30, 2024 17:37
-
-
Save LeeeeT/f25251ed2ca9c3777c0519d63dccdd60 to your computer and use it in GitHub Desktop.
Finding pythagorean triples ("I hate my life" edition)
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
from sys import setrecursionlimit | |
setrecursionlimit(10000) | |
lift2 = lambda identity: lambda apply: lambda function: lambda first: apply(apply(identity(function))(first)) | |
bind = lambda map: lambda join: lambda value: lambda function: join(map(function)(value)) | |
guard = lambda neutral: lambda identity: lambda boolean: boolean(neutral)(identity(unit)) | |
function_identity = lambda value: lambda _: value | |
function_flip = lambda function: lambda first: lambda second: function(second)(first) | |
unit = lambda unit: unit | |
boolean_no = lambda no: lambda yes: no | |
boolean_yes = lambda no: lambda yes: yes | |
boolean_to_python_bool = lambda boolean: boolean(False)(True) | |
pair = lambda first: lambda second: lambda pair: pair(first)(second) | |
pair_first = lambda pair: pair(lambda first: lambda second: first) | |
pair_second = lambda pair: pair(lambda first: lambda second: second) | |
natural_zero = lambda zero: lambda successor: zero | |
natural_successor = lambda natural: lambda zero: lambda successor: successor(natural(zero)(successor)) | |
natural_predecessor = lambda natural: pair_first(natural(pair(natural_zero)(natural_zero))(lambda state: pair(pair_second(state))(natural_successor(pair_second(state))))) | |
natural_one = natural_successor(natural_zero) | |
natural_two = natural_successor(natural_one) | |
natural_neutral = natural_zero | |
natural_add = lambda addend: lambda augend: addend(augend)(natural_successor) | |
natural_subtract = lambda subtrahend: lambda minuend: subtrahend(minuend)(natural_predecessor) | |
natural_multiply = lambda multiplier: lambda multiplicand: multiplier(natural_zero)(natural_add(multiplicand)) | |
natural_exponentiate = lambda exponent: lambda base: exponent(natural_one)(natural_multiply(base)) | |
natural_square = natural_exponentiate(natural_two) | |
natural_absolute_difference = lambda first: lambda second: natural_add(natural_subtract(first)(second))(natural_subtract(second)(first)) | |
natural_is_zero = lambda natural: natural(boolean_yes)(function_identity(boolean_no)) | |
natural_equal = lambda first: lambda second: natural_is_zero(natural_absolute_difference(first)(second)) | |
natural_to_python_int = lambda natural: natural(0)((1).__add__) | |
python_int_to_natural = lambda int: natural_successor(python_int_to_natural(int - 1)) if int else natural_zero | |
list_empty = lambda empty: lambda constructor: empty | |
list_constructor = lambda head: lambda tail: lambda empty: lambda constructor: constructor(head)(tail(empty)(constructor)) | |
list_append = lambda item: lambda list: list(list_constructor(item)(list_empty))(list_constructor) | |
list_neutral = list_empty | |
list_add = lambda addend: lambda augend: addend(augend)(list_constructor) | |
list_map = lambda function: lambda list: list(list_empty)(lambda head: lambda tail: list_constructor(function(head))(tail)) | |
list_identity = lambda value: list_constructor(value)(list_empty) | |
list_apply = lambda list_function: lambda list_value: list_function(list_empty)(lambda head: lambda tail: list_add(list_map(head)(list_value))(tail)) | |
list_lift2 = lift2(list_identity)(list_apply) | |
list_take_after = list_lift2(function_flip(function_identity)) | |
list_join = lambda list: list(list_neutral)(list_add) | |
list_bind = bind(list_map)(list_join) | |
list_guard = guard(list_neutral)(list_identity) | |
list_reverse = lambda list: list(list_empty)(list_append) | |
list_countdown = lambda natural: pair_first(natural(pair(list_empty)(natural_zero))(lambda state: pair(list_constructor(pair_second(state))(pair_first(state)))(natural_successor(pair_second(state))))) | |
list_count = lambda natural: list_reverse(list_countdown(natural)) | |
list_count_from = lambda start: lambda natural: list_map(natural_add(start))(list_count(natural)) | |
list_range = lambda start: lambda stop: list_count_from(start)(natural_subtract(start)(stop)) | |
list_to_python_list = lambda list: list([])(lambda head: lambda tail: [head, *tail]) | |
pythags = \ | |
list_bind(list_range(python_int_to_natural(1))(python_int_to_natural(20)))(lambda z: | |
list_bind(list_range(python_int_to_natural(1))(z))(lambda x: | |
list_bind(list_range(x)(z))(lambda y: | |
list_take_after(list_guard(natural_equal(natural_square(z))(natural_add(natural_square(y))(natural_square(x)))))( | |
list_identity(list_constructor(x)(list_constructor(y)(list_constructor(z)(list_empty)))) | |
)))) | |
print(list_to_python_list(list_map(lambda list: list_to_python_list(list_map(natural_to_python_int)(list)))(pythags))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment