4
Recursively define a "valid arithmetic expression":
- Any natural number is a valid arithmetic expression.
- If
s
is a valid arithmetic expression, then so is(-s)
. - If
p
andq
are valid arithmetic expressions, then so is(p+q)
.
In the above, a "natural number" is recursively defined as follows:
0
1
2
3
4
5
6
7
8
9
are natural numbers.- If a natural number
n
is not0
, thenn0
n1
n2
n3
n4
n5
n6
n7
n8
n9
are natural numbers.
Alternatively, via the regex /^(0|[1-9][0-9]*)$/
Examples of valid arithmetic expressions:
0
314
(-7)
(0+0)
(314+(-314))
Examples of invalid arithmetic expressions:
01
-5
5+10
The challenge is to print every valid arithmetic expression line by line, i.e. create a program that will output strings line by line with the constraint that every valid arithmetic expression is eventually printed, and that no invalid arithmetic expression is ever printed.
The valid arithmetic expressions can be printed more than once.
The challenge is to do so in as few bytes as possible, since this is code-golf.
Standard loopholes apply.
Can we expect the input to be non-empty? – LiefdeWen – 2018-04-16T12:25:01.277
2The output should be infinite, presumably? How can we demonstrate that every valid expression will be printed? – Phil H – 2018-04-16T12:46:50.353
1@PhilH well you can challenge an answer that doesn't meet the criterion and the author of the answer would have to justify it... – Leaky Nun – 2018-04-16T12:49:48.560
@LiefdeWen you mean empty? – Leaky Nun – 2018-04-16T12:49:56.963
Suppose someone writes an entry which just enumerates the natural numbers, it would only finish that after infinity... – Phil H – 2018-04-16T13:27:02.133
1@PhilH then the entry is invalid. – Leaky Nun – 2018-04-16T13:27:28.207
1@PhilH That doesn't satisfy "every valid arithmetic expression is eventually printed", as
(-0)
is never printed. – user202729 – 2018-04-16T13:40:03.540@user202729 I could claim it would do so eventually, but also isn't this supposed to be code rather than theory of code? What's the real difference between a program that prints all the positive natural numbers then starts on the negative numbers, and one that just prints the positive numbers? The outputs are the same for all time... – Phil H – 2018-04-16T15:08:31.787
1@PhilH Seems that you misunderstood the rules? The validity criteria is "for all expression E, there exists a (finite) natural number N, such that the N'th line printed is E" Your example does not satisfy that. – user202729 – 2018-04-16T15:19:54.210