A recursive approach is suggested if you want follow the truth mathematical definition of natural number. My implementation uses strings and not lists.
Generating natural numbers:
- following the mathematical set-theoretical definition
def natural_number(n):
def natural_number_(n):
if n == 0: return '[]'
return '{N}, [{N}]'.format(N=natural_number_(n-1))
return '[{}]'.format(natural_number_(n))
for i in range(4):
print(i, natural_number(i))
Output
0 []
1 [[]]
2 [[], [[]]]
3 [[], [[]], [[], [[]]]]
4 [[], [[]], [[], [[]]], [[], [[]], [[], [[]]]]]
- following the unit test ( the unnatural natural numbers):
def unnatural_number(n):
if n == 0: return '[]'
return '[{N}, [{N}]]'.format(N=unnatural_number(n-1))
for i in range(4):
print(i, unnatural_number(i))
Output
0 []
1 [[], [[]]]
2 [[[], [[]]], [[[], [[]]]]]
3 [[[[], [[]]], [[[], [[]]]]], [[[[], [[]]], [[[], [[]]]]]]]
4 [[[[[], [[]]], [[[], [[]]]]], [[[[], [[]]], [[[], [[]]]]]]], [[[[[], [[]]], [[[], [[]]]]], [[[[], [[]]], [[[], [[]]]]]]]]]
Check if the unnatural_number
satisfies the requirements
check = unnatural_number(4) == '[[[[[], [[]]], [[[], [[]]]]], [[[[], [[]]], [[[], [[]]]]]]], [[[[[], [[]]], [[[], [[]]]]], [[[[], [[]]], [[[], [[]]]]]]]]]'
print(check)
# True
Considerations
- the difference between the
natural_number
and unnatural_number
is the most outside bracket:
'{N}, [{N}]'
'[{N}, [{N}]]'
<- not really natural!
- such construction of natural numbers was originally pointed out by von Neumann