Skip to main content
added 1393 characters in body
Source Link

1+ (EOF returns 0), 17 bytes

1##.,"1<1+#+(|:|;()) 

Try it online!

The TIO link uses integer I/O and a so-called input template (I'm going to use it for all my 1+ answers from now on) because 1. The interpreter reads input and source code from the same place for some reason and 2. It does not support EOF so I have to simulate it by manually putting the EOF in the input and 3. The NUL character cannot be placed in the input so I have to use integers.

Explanation

This supports all printable ASCII (and LF and CR), and the question didn't say anything about other unprintable characters so I guess that is okay.

The first part of the program, 1##,"1<1+#+, reads the input to the stack. Here we save 2 bytes by comparing n <= 1 (1<) and not n <= 0 (which is 1+1<) since in 1+, 1 is the only literal, which indicates pushing a 0 is longer.

After the loop, we have the input and a trailing EOF (0) on the stack. We get rid of the EOF by adding the top two numbers (this doesn't work for empty input, but again, the question didn't say anything about it!) and since a + 0 = a it will not affect the rest of the input.

The second part, (|;()), is a simple recursive function that (pop and) outputs top of stack and it terminates with error when there are no more elements to output. I used recursion because loop is longer.

Supports all ASCII characters except NUL, 19 bytes

1##,"1+1<1+#+(|;()) 

1+, 17 bytes

1##."1<1+#+(|:()) 

Try it online!

1+ (EOF returns 0), 17 bytes

1##,"1<1+#+(|;()) 

Try it online!

The TIO link uses integer I/O and a so-called input template (I'm going to use it for all my 1+ answers from now on) because 1. The interpreter reads input and source code from the same place for some reason and 2. It does not support EOF so I have to simulate it by manually putting the EOF in the input and 3. The NUL character cannot be placed in the input so I have to use integers.

Explanation

This supports all printable ASCII (and LF and CR), and the question didn't say anything about other unprintable characters so I guess that is okay.

The first part of the program, 1##,"1<1+#+, reads the input to the stack. Here we save 2 bytes by comparing n <= 1 (1<) and not n <= 0 (which is 1+1<) since in 1+, 1 is the only literal, which indicates pushing a 0 is longer.

After the loop, we have the input and a trailing EOF (0) on the stack. We get rid of the EOF by adding the top two numbers (this doesn't work for empty input, but again, the question didn't say anything about it!) and since a + 0 = a it will not affect the rest of the input.

The second part, (|;()), is a simple recursive function that (pop and) outputs top of stack and it terminates with error when there are no more elements to output. I used recursion because loop is longer.

Supports all ASCII characters except NUL, 19 bytes

1##,"1+1<1+#+(|;()) 
Source Link

1+, 17 bytes

1##."1<1+#+(|:()) 

Try it online!