172
\$\begingroup\$

A truth-machine (credits goes to this guy for coming up with it) is a very simple program designed to demonstrate the I/O and control flow of a language. Here's what a truth-machine does:

  • Gets a number (either 0 or 1) from STDIN.

  • If that number is 0, print out 0 and terminate.

  • If that number is 1, print out 1 forever.

Challenge

Write a truth-machine as described above in your language of choice. The truth-machine must be a full program that follows these rules:

  • take input from STDIN or an acceptable alternative
    • If your language cannot take input from STDIN, it may take input from a hardcoded variable or suitable equivalent in the program
  • must output to STDOUT or an acceptable alternative
    • If your language is incapable of outputting the characters 0 or 1, byte or unary I/O is acceptable.
  • when the input is 1, it must continually print 1s and only stop if the program is killed or runs out of memory
  • the output must only be either a 0 followed by either one or no newline or space, or infinite 1s with each 1 followed by either one or no newline or space. No other output can be generated, except constant output of your language's interpreter that cannot be suppressed (such as a greeting, ANSI color codes or indentation). Your usage of newlines or spaces must be consistent: for example, if you choose to output 1 with a newline after it all 1s must have a newline after them.

  • if and only if your language cannot possibly terminate on an input of 0 it is acceptable for the code to enter an infinite loop in which nothing is outputted.

Since this is a catalog, languages created after this challenge are allowed to compete. Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language. Other than that, all the standard rules of must be obeyed. Submissions in most languages will be scored in bytes in an appropriate preexisting encoding (usually UTF-8).

Catalog

The Stack Snippet at the bottom of this post generates the catalog from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

<style>body { text-align: left !important} #answer-list { padding: 10px; width: 290px; float: left; } #language-list { padding: 10px; width: 320px; float: left; } table thead { font-weight: bold; } table td { padding: 5px; }</style><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="language-list"> <h2>Shortest Solution by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr> </thead> <tbody id="languages"> </tbody> </table> </div> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr> </thead> <tbody id="answers"> </tbody> </table> </div> <table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table><script>var QUESTION_ID = 62732; var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk"; var OVERRIDE_USER = 12012; var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page; function answersUrl(index) { return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER; } function commentUrl(index, answers) { return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER; } function getAnswers() { jQuery.ajax({ url: answersUrl(answer_page++), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { answers.push.apply(answers, data.items); answers_hash = []; answer_ids = []; data.items.forEach(function(a) { a.comments = []; var id = +a.share_link.match(/\d+/); answer_ids.push(id); answers_hash[id] = a; }); if (!data.has_more) more_answers = false; comment_page = 1; getComments(); } }); } function getComments() { jQuery.ajax({ url: commentUrl(comment_page++, answer_ids), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { data.items.forEach(function(c) { if (c.owner.user_id === OVERRIDE_USER) answers_hash[c.post_id].comments.push(c); }); if (data.has_more) getComments(); else if (more_answers) getAnswers(); else process(); } }); } getAnswers(); var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/; var OVERRIDE_REG = /^Override\s*header:\s*/i; function getAuthorName(a) { return a.owner.display_name; } function process() { var valid = []; answers.forEach(function(a) { var body = a.body; a.comments.forEach(function(c) { if(OVERRIDE_REG.test(c.body)) body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>'; }); var match = body.match(SCORE_REG); if (match) valid.push({ user: getAuthorName(a), size: +match[2], language: match[1], link: a.share_link, }); else console.log(body); }); valid.sort(function (a, b) { var aB = a.size, bB = b.size; return aB - bB }); var languages = {}; var place = 1; var lastSize = null; var lastPlace = 1; valid.forEach(function (a) { if (a.size != lastSize) lastPlace = place; lastSize = a.size; ++place; var answer = jQuery("#answer-template").html(); answer = answer.replace("{{PLACE}}", lastPlace + ".") .replace("{{NAME}}", a.user) .replace("{{LANGUAGE}}", a.language) .replace("{{SIZE}}", a.size) .replace("{{LINK}}", a.link); answer = jQuery(answer); jQuery("#answers").append(answer); var lang = a.language; lang = jQuery('<a>'+lang+'</a>').text(); languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang.toLowerCase(), user: a.user, size: a.size, link: a.link}; }); var langs = []; for (var lang in languages) if (languages.hasOwnProperty(lang)) langs.push(languages[lang]); langs.sort(function (a, b) { if (a.lang_raw > b.lang_raw) return 1; if (a.lang_raw < b.lang_raw) return -1; return 0; }); for (var i = 0; i < langs.length; ++i) { var language = jQuery("#language-template").html(); var lang = langs[i]; language = language.replace("{{LANGUAGE}}", lang.lang) .replace("{{NAME}}", lang.user) .replace("{{SIZE}}", lang.size) .replace("{{LINK}}", lang.link); language = jQuery(language); jQuery("#languages").append(language); } }</script>

\$\endgroup\$
18
  • 3
    \$\begingroup\$ Assuming any behaviour is fine for all invalid inputs? \$\endgroup\$
    – Cruncher
    Nov 3, 2015 at 17:33
  • 3
    \$\begingroup\$ @Cruncher Yes, the only inputs you should expect to get are 0 and 1. \$\endgroup\$ Nov 3, 2015 at 17:38
  • 5
    \$\begingroup\$ Catalog is borked. \$\endgroup\$ Nov 6, 2015 at 15:18
  • 2
    \$\begingroup\$ Catalog appears to consider Bf and bf to be different languages. \$\endgroup\$ Nov 10, 2015 at 1:13
  • 1
    \$\begingroup\$ Is one allowed to print an infinite number of 1s? That is, not print out 1s forever, but print out an infinite list of 1s all at once? This would be theoretically demonstrated, but the language has no notion of printing more than once. It just generates an output. So is it okay if it runs indefinitely, but would print infinite 1s given infinite time (to finish executing)? \$\endgroup\$
    – AviFS
    Apr 26, 2020 at 1:42

491 Answers 491

1
8 9
10
11 12
17
1
\$\begingroup\$

Ru, 11 9 bytes

»Ϟα;¿{»α}

This look way too long

\$\endgroup\$
1
\$\begingroup\$

Python 2, 33 bytes

Golfing suggestions welcome :)

x=input()
while x:print 1
print 0
\$\endgroup\$
8
  • \$\begingroup\$ This doesn't work. \$\endgroup\$
    – ABcDexter
    Sep 1, 2016 at 9:24
  • 1
    \$\begingroup\$ @ABcDexter Ah you're right, I forgot the int. I actually initially wrote this in python2 where you don't need the int conversion and added the parentheses in later. Check it now. \$\endgroup\$
    – gowrath
    Sep 1, 2016 at 9:26
  • \$\begingroup\$ Somebody already beat you to the Python 2 solution. \$\endgroup\$
    – user45941
    Sep 1, 2016 at 9:31
  • 1
    \$\begingroup\$ @Mego Apparently, no. As the other code takes input again and again in the while condition. \$\endgroup\$
    – ABcDexter
    Sep 1, 2016 at 9:33
  • 1
    \$\begingroup\$ There's also this one \$\endgroup\$
    – user45941
    Sep 1, 2016 at 9:34
1
\$\begingroup\$

Straw, 17 bytes

(1>:&)(:&) <1='0>
\$\endgroup\$
1
\$\begingroup\$

Logicode, 43 bytes

circ l(a)->1+l(a)
cond binp->out l(a)/out 0

Logicode can't output while in an infinite circuit loop, so this program will output ALL of the 1's after the infinite loop (when the program encounters a 1).

If a truth machine that goes into an infinite loop when 1 is entered and has no output is acceptable, the code can be shorter by 2 bytes:

circ l(a)->1+l(a)
cond binp->out l(a)/out 0
\$\endgroup\$
1
\$\begingroup\$

braingasm, 5 bytes

;:[:]

The braingasm instruction ; and : are very similar to the brainfuck instructions , and ..

; gets some input from stdin, reads it as an integer, and stores the integer value in the current cell. : prints the value of the current formatted as an integer. Like in brainfuck, [] loops while the current cell is non-zero.

\$\endgroup\$
1
\$\begingroup\$

reticular, 8 6 bytes

[do?!]

Try it online!

This is a function. g calls the function with the TOS as input. do outputs without popping, ?! skips the next char if it's 1. Because of the way functions work, the actual constructed function is:

do?!;

This is made in a child instance of the program. Thus, if the TOS is 0, then it terminates (exits function).


Previous versions:

8 bytes:

indp?!;!

11 bytes:

in@@p;
p1 <

This goes right by @@ if 0, and up if 1 (wrapping around to <) and prints 1 infinitely. It's in better style to do it this way, y'know, more readable. Because 2D languages are easy to read.

\$\endgroup\$
1
\$\begingroup\$

PHP, 30 33 bytes

do{echo$o=$argv[1];}while($o);

Oops, didn't see the 7 other pages, one of which contains a better PHP entry.

Old 1

do{echo$argv[1];}while($argv[1]);
\$\endgroup\$
1
\$\begingroup\$

Javascript, 33 32 Bytes

i=prompt();do{alert(i)}while(+i)

uses the +i syntax (thanks ETHproductions)

Old 1

i=prompt();do{alert(i)}while(i>0)
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 0 is a falsy value in JavaScript, so you can shorten this by one byte with i=prompt();do{alert(i)}while(+i) \$\endgroup\$ Nov 1, 2016 at 16:26
  • \$\begingroup\$ Nice one, wasn't aware of that short cut! \$\endgroup\$
    – CT14.IT
    Nov 1, 2016 at 16:31
1
\$\begingroup\$

D - 87 bytes

void main(){import std.stdio,std.conv;int i=readln[0..$-1].to!int;do i.write;while(i);}
\$\endgroup\$
1
\$\begingroup\$

Fuzzy Octo Guacamole, 8 bytes

^X({@}X)

^ pushes input, X prints the top item on the stack. ( starts an infinite loop. {@} ends the program (@) only if the top item on the stack is falsy, like 0. The X prints the top item on the stack (again), and the ) ends the loop.

So it repeats, printing and checking forever.

\$\endgroup\$
1
  • \$\begingroup\$ "languages created after this challenge are allowed to compete" \$\endgroup\$ Nov 17, 2016 at 8:27
1
\$\begingroup\$

PHP, 20 bytes

<?L:echo$i;if($i)goto L;# 24 bytes
<?do echo$i;while($i);  # Martijn´s first version golfed 24->22 bytes
<?=$i;while($i)echo$i;  # Martijn´s second version golfed 24->22 bytes
<?while($i|!print$i);   # 21 bytes
<?while($i&print$i);    # 20 bytes

Requires PHP <4.2 or PHP<7 with --d register_globals=0.
Save to file, call in browser with <scriptpath>?i=<value>.

explanation for the last version:

print$i is evaluated in any case (no short circuit for bitwise operations).
print always returns true, which, when cast to int (by the bitwise and) evaluates to 1.
For $i=0, 0&1 is 0, hence false and the loop exits. for $i=1, 1&1 is 1 and the loop continues.

shortest version(s) for current PHP, 29 bytes:

while(($i=$argv[1])&print$i);
while((print$i=$argv[1])&$i);
for($i=$argv[1];$i&print$i;);

Run with php -r '<code>' <value>.

\$\endgroup\$
1
\$\begingroup\$

Haystack, 9 bytes

id?v
|o<o

Try it online!

I'm aware that there already is a Haystack answer, but that answer uses the older version of Haystack, this answer uses Haystack 2016 (and it's much shorter)

Explanation

id           Take input and duplicate it
  ?          If input is truthy (1) continue, otherwise (0) go down

If input is truthy...

?v           Go down
 o           Output number
             Since this is a 2D language, the IP wraps around and does this infinitely
             Also since Haystack (new) doesn't pop the top of stack after outputting
              1 can be printed forever without needing to duplicate it

Otherwise...

  ?
  <          Go left
 o           Output number
|            Exit program
\$\endgroup\$
1
\$\begingroup\$

Pushy, 3 bytes

#$#

Extremely simple:

#    \ Print input
 $   \ While input != 0:
  #  \   Print input

An input of 0 will print, skip over the while loop, and terminate, whilst any other number is considered truthy and will be printed infinitely.

\$\endgroup\$
1
  • \$\begingroup\$ ...what's with the downvote? \$\endgroup\$
    – FlipTack
    Jan 2, 2017 at 14:19
1
\$\begingroup\$

QBIC, 10 bytes

:{?a~a|\_X

Explanation:

:    Reads a number from the command line and names it 'a'
{    DO - infinite loop
?a   Print 'a'
~a   IF 'a': 0 is seen as false, 1 as true
|    THEN: Empty THEN block, we want to act on FALSE
\_X  ELSE exit the program.
     _X accepts one implicit parameter, and prints that parameter on exit.
     Since no parameter is given, nothing gets printed.
[IF and DO are implicitly closed by QBIC]
\$\endgroup\$
1
\$\begingroup\$

Threead, 5 bytes (Noncompeting)

I[o]o

read, while not 0 output, output

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Huh, I hadn't even thought about having the single output be after the infinite output, in any language... \$\endgroup\$ Jan 30, 2017 at 3:00
  • \$\begingroup\$ @ETHproductions it doesn't make a difference, actually. Io[o] works too. \$\endgroup\$
    – Pavel
    Jan 30, 2017 at 3:22
  • \$\begingroup\$ Also this is a catalogue so it is competing (kinda) \$\endgroup\$ Jun 4, 2017 at 18:12
1
\$\begingroup\$

Pyramid, 14 bytes

+1 byte to specify starting on stack 1 (originally 13 bytes).

1<
b
---
?<
u

Pyramid is my new-old language, which is based off Stackylogic. It has a bunch of extra commands that will (hopefully) make it TC, which include moving up and down multiple "stacks".

Explanation:

1<   1: Move down the stack, and push 0 to the output stack
b    Break: outputs the stack, and resets it - also runs the stack again
---
?<   Input: 1, 0: If it's 0, move up, push 0 to the output stack, 
     terminate the program and print the stack
u    If the input is 1, move up to stack 0
\$\endgroup\$
1
  • \$\begingroup\$ The leaderboard snippet is reading your header as saying that this is 1 byte. You should probably move the part in parentheses to the second line (and, if I understand it right, change the header to say 15 bytes). \$\endgroup\$
    – DLosc
    Feb 8, 2017 at 21:57
1
\$\begingroup\$

Alice, 8 bytes

i.o.&#@#

Try it online!

Explanation

i     Read a byte X from STDIN (gives 48 for input 0, 49 for input 1).
.o    Duplicate X and print that byte back to STDOUT.
.     Duplicate X again.
&#    Skip X commands. Since the program contains 8 commands, if X = 48, 
      this doesn't really do anything. The IP will just loop through the 
      code six times while skipping all commands. But if X = 49, it skips
      one more command, so the next command is skipped.
@     Terminate the program (only gets executed for input 0).
#     Skip the i at the beginning of the next iteration.

This code loops indefinitely, unless the @ terminates it on the first iteration.

\$\endgroup\$
1
\$\begingroup\$

OIL, 25 bytes

Straightforward, but nevertheless annotated:

5  # read into cell 0
0
10 # if cell 0 is equal to 0 (from cell 1), go to cell 11 (*) else 7 (&)
0
1
11
7
4  # print what's in cell 4 (a zero) &
4
6  # jump to cell 7 (&)
7
4  # print implicit 0 *
\$\endgroup\$
1
\$\begingroup\$

Javascript (Node), 40 bytes

for(;console.log(v=process.argv[2])|v;);

In Javascript for the browser this can be 29 bytes

v=prompt();for(;alert(v)|v;);
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ May 7, 2017 at 20:51
1
\$\begingroup\$

Triangular, 6 bytes

$.(]%<

Try it online!

This formats into the triangle:

  $
 . (
] % <

The commands that are executed (without control flow) are $(%]. Pretty simple.

  • $ read input as integer
  • ( open loop
  • % print as integer
  • ] jump back to loop if top of stack is truthy
\$\endgroup\$
1
\$\begingroup\$

ILL, 19 bytes

/RnN~
>1nR\
\   /

ILL (inverse linear law) is new right now, so this represents virtually all the instructions/groups of instructions it has at the time of posting this. The main idea though, is that the program is run by light, which reflects off of mirrors and decrease in intensity as it moves on (obeying the inverse linear law, because its 2d). Data is primarily stored in the intensity of the light, which can sort of be used as a control structure, since light with an intensity < .5 dims and does not move on to the next tick. This means that different intensities of light move different lengths. This program utilizes the dimming behavior to either halt after printing 0, or prints 1 and continues. Basically, it takes a numerical input (side note: instructions that set intensity actually do so after the current tick has run, and they also persist the light over to the next instruction, no matter what intensity it has), prints it, and then re-intensifies and enters a loop. However, the light is only re-intensified if it reaches the re-intensify instruction, which light with an intensity of 0 will not. More detailed explanation (also a valid ILL program):

/RnN~
>1nR\
\   /
#END OF FILE#
Truth machine.

~ : emits light horizontally at the start of a program.
N : takes numerical input, set light intensity to the input.
n : produces numerical output. If the light has an intensity of 0 here, it will dim after n outputs and not move on.
R : re-intensify, if the light made it here, it can now continue into the loop.
> : double/splitting mirror, allows the light from the above / mirror to enter the loop and move right.
1 : set intensity to 1.
n : numerical output.
R : re-intensify.

the remaining \, /, and \ reflect the light back to > where it can start the loop again
\$\endgroup\$
1
\$\begingroup\$

C# (.NET Core), 80 79 bytes

class p{static void Main(string[]I){do{Console.Write(I[0]);}while(I[0]=="1");}}

Try it online!

I've seen some disagreement about whether arguments count as STDIN. "No" seemed more popular, but if it were acceptable then this would save 15 bytes over the previous C# answer.

Saved 1 byte by removing an unnecessary space

\$\endgroup\$
1
\$\begingroup\$

Cubically, 6 bytes

According to the spec, this should work:

$(%7)7
$      - Get user input as number (either 1 or 0, according to the rules)
 (  )7 - do ... while input value is nonzero
  %7   - Output the input as a number

However, TIO seems to continue the loop even if the input is 0. The Lua interpreter handles it correctly, though.

\$\endgroup\$
3
  • \$\begingroup\$ $:7(%7)6 works on TIO for now. It should behave identically (instead of looping based on the value in 7, it sets 6 to that value and loops on that) but it doesn't, meaning there's a bug in the interpreter. \$\endgroup\$ Aug 5, 2017 at 12:10
  • \$\begingroup\$ Works offline and Dennis should be pulling. Good job, can't believe I didn't notice this! I always thought a truth machine had to print 1. \$\endgroup\$
    – MD XF
    Aug 5, 2017 at 17:06
  • \$\begingroup\$ @MDXF It does print 1. The rules state that the input will either be a 1 or a 0 :P To make it print 1 always, just do :1/1%6 instead of %7 \$\endgroup\$
    – TehPers
    Aug 7, 2017 at 19:47
1
\$\begingroup\$

8th, 58 bytes

Code

: f getc '0 - dup 0 1 between if repeat dup . while then ;

Explanation

: f - Start of word definition

getc - Get input from STDIN

'0 - - Subtract 0's ASCII code to determine numeric input

dup - Duplicate result to be used to run loop

0 1 between if - Check if input is either 0 or 1

repeat dup . while - Print either 0 once or 1 ad infinitum

then ; - Exit word

If input is not either 0 or 1, exit with no output. At the end of the program the stack is not empty.

\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 20 Bytes

Anonymous VBE immediate window function that takes input from range [A1] and output 1, infinitely if [A1] else 0

While[A1]:?1:Wend:?0
\$\endgroup\$
1
\$\begingroup\$

Whitespace, 40 bytes

Note that the only valid characters are space, tab, and linefeed, so they are represented here with S, T, and L, respectively. Comments are also given.

I used this interpreter to write and test this program (warning: crashes browser window upon input of 1).

SS L   # push 0
SLS    # duplicate
TLTT   # readi
TTT    # retrieve
LSS L  #label "" (blank label name, why not?)
SLS    # duplicate
SLS    # duplicate
TLST   # printi
SS STL # push 1
TSST   # subtract
LTS L  # jz label "" (jump to blank label if input-1=0)

Three consecutive linefeeds (LLL) are usually required to end program execution at a point, but that interpreter doesn't seem to mind if you leave them out. If they have to be added in, the score would instead be 43 bytes.

\$\endgroup\$
1
  • \$\begingroup\$ The interpreter defines the language, so the missing LLL should be completely fine. \$\endgroup\$ Sep 4, 2017 at 5:32
1
\$\begingroup\$

Pyth, 5 bytes

Like my Python answer, but in Pyth

WQ1)0

Explanation:

WQ1)      While Q, print 1. 1 = true, 0 = false; end While
0         Print 0

If input is 1, 0 is never reached, if input is 0, 1 is never reached.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

4, 28 bytes

3.61448701501132011483250194

Try it online!

Explanation:

3.            Start Program
6 14 48       Set the value of memory cell 14 to 48 (the ASCII code of '0')
7 01          Read in one byte to memory cell 01
5 01          Print byte from memory cell 01
1 32 01 14    Set the value of memory cell 32 to cell 01 - cell 14
8 32          Start loop: if cell 32=0, skip to the end
5 01          Print byte from memory cell 01
9             End loop
4             End program
\$\endgroup\$
1
\$\begingroup\$

Implicit, 5 4 bytes

$(%)

Try it online! Prints 00, which is equal to 0 Pretty simple:

$(%)
$      « integer input               »;
 (.)   « do..while top of stack true »;
  %    «  print top of stack         »;
       « implicit integer output     »;
\$\endgroup\$
1
\$\begingroup\$

Husk, 5 bytes

¶?∞;I

Try it online!

Explanation

 ?  I  -- if the input is truthy
  ∞    --   repeat it infinitly many times
   ;   --   else create singleton list
¶      -- join by newlines

Thanks @H.PWiz for golfing off 1 byte!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 5 bytes: ¶?∞;± or, if string input is valid: ?∞;i \$\endgroup\$
    – H.PWiz
    Nov 21, 2017 at 21:06
1
8 9
10
11 12
17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.