173
\$\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\$
19
  • 3
    \$\begingroup\$ Assuming any behaviour is fine for all invalid inputs? \$\endgroup\$
    – Cruncher
    Commented Nov 3, 2015 at 17:33
  • 3
    \$\begingroup\$ @Cruncher Yes, the only inputs you should expect to get are 0 and 1. \$\endgroup\$ Commented Nov 3, 2015 at 17:38
  • 5
    \$\begingroup\$ Catalog is borked. \$\endgroup\$ Commented Nov 6, 2015 at 15:18
  • 2
    \$\begingroup\$ Catalog appears to consider Bf and bf to be different languages. \$\endgroup\$ Commented 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
    Commented Apr 26, 2020 at 1:42

495 Answers 495

0
\$\begingroup\$

VTL-2, 25 bytes

1 A=?
2 ?=A
3 #=4-2*A

Line numbers always take up two bytes in VTL, hence the byte count discrepancy.

This should work in VTL-1 as well, but I don't have an interpreter to ensure that is the case. I've tested this under a VTL-2 interpreter running on an Altair 8800 simulator, both sourced from here. Here's a PDF manual for VTL-2.

VTL was a small (<800 bytes!) and simple language for the Altair 8800 and 680 machines. Its creative use of system variables helped to keep it small. ? represents both input and output. Note that this truth machine outputs no line breaks, as those always need to be manually printed (by printing an empty string, ?=""). # represents line number, and can be used to retrieve the current line number or assigned to form a goto. That should be enough to make this truth machine make sense...

1 A=?         ) Put input into variable A
2 ?=A         ) Print the contents of variable A
3 #=4-2*A     ) Goto 4-(2*A)... So, (nonexistent) line 4 if input was 0
              ) Or back to line 2 if input was 1. Yes, `)` is the comment character.
\$\endgroup\$
0
\$\begingroup\$

Aheui, 28 bytes

붕박누망홰
차봇멍보

Try it online!

Additionally, I found bug of TIO with Aheui : Aheui is befunge-like, but initial direction is up-to-down, but TIO's initial direction is right-to-left, so my original code got error.

\$\endgroup\$
0
\$\begingroup\$

EZLang, 105 bytes

MEM(0)
SET(-1)
MEM(1)
GET()
SET(-1, 1)
JLZ(12)
SET(1, 1)
MEM(1)
PRT()
MEM(0)
JLZ(8)
SET(1, 1)
PRT()
HLT()

Self-made esolatic language.

\$\endgroup\$
0
\$\begingroup\$

33, 4 bytes

O[o]

Explanation:

0 input:

O    | Get input
  o  | Print it
 [ ] | Loop ends because accumulator is 0

1 input:

O    | Get input
 [ ] | While the accumulator is 1 (forever)
  o  | Print it
\$\endgroup\$
0
\$\begingroup\$

Befunge-93, 7 bytes

&>:.:_@

Try it online!

_ is the horizontal if in befunge - conveniently, it sends the IP right if the top of the stack is 0 (so it hits @ to end the program), and left if the top of the stack is nonzero (which puts it in an infinite loop).

\$\endgroup\$
1
  • \$\begingroup\$ This seems to be a duplicate of this answer. \$\endgroup\$
    – Mr. Xcoder
    Commented Aug 26, 2019 at 19:06
0
\$\begingroup\$

Runic Enchantments, 6 bytes

i:?@:$

Try it online!

? is a conditional jump command that skips n instructions where n is a value popped from the top of the stack. If that value is 0, we dump the stack and terminate. If its 1 we skip that instruction and print the value, leaving a copy on the stack, then loop. i does nothing if there is no more input to read.

\$\endgroup\$
0
\$\begingroup\$

JavaScript, 32 bytes

do{alert(x=x||prompt())}while(x)
\$\endgroup\$
0
\$\begingroup\$

Unsuspected-hangul, 187 bytes

ㅀㄱ ㄱㅇㄱ ㅅㅅㅎㄴ ㄳㅎㄴㅎ ㄱㅀㄷ ㄱㅇㄱ ㅁㅈㅎㄴ ㅈㅀㄴ ㄱㅇㄴ ㄴㅇㅎㄴ ㅂㄱㅎㄱ ㄳㅎㄴ ㄱㅇㄴ ㄴ ㄴㅎㄷㅎㄷㅎ ㄱㅀㄷㅎ ㄱㅀㄷ

You can try it here

Note that it use pop-up for standard output, and you have to close tap to terminate the program.

How does it work?

ㄹ ㅎㄱ [ㄱㅇㄱ ㅅㅅㅎㄴ ㄱㅅㅎㄴ ㅎ] ㄱㄹㅎㄷ bind(input(), int)
    [def print_and_recursion(x) num -> IO(nil)
    ㄱㅇㄱ ㅁㅈㅎㄴ ㅈㄹㅎㄴ print(str(x))
        [def recursion_part(not_used) nil -> IO(nil)
        (ㄱㅇㄴ ㄴㅇ ㅎㄴ) print_and_recursion(x)
        (ㅂㄱㅎㄱ ㄱㅅㅎㄴ) IO(nil)
        (ㄱㅇㄴ ㄴ ㄴㅎㄷ) ㅎㄷ if(x == 1) then... else...
    ㅎ]
    ㄱㄹㅎㄷ bind(print(str(x)), recursion_part)
ㅎ]
ㄱㄹㅎㄷ bind(bind(input(), int), print_and_recursion)

Unsuspected-hangul(평범한 한글) is Korean character-based functional esolang.

\$\endgroup\$
0
\$\begingroup\$

Wren, 55 bytes

Fn.new{|x|
while(x){
System.print(x)
if(x==0)Fn.x()
}
}

Try it online!

Explanation

Fn.new{|x|      // As usual, new anonymous function x
                // Newline as we are entering statements, not an expression
while(x){       // While the input x is true:
                // (Everything other than false, the null string, and the
                // empty list is true)
System.print(x) // Output x to the console with a newline
if(x==0)Fn.x()  // If the input is 0 then exit the program
}
}
\$\endgroup\$
0
\$\begingroup\$

Fugue, 120 bytes

00000000: 4d54 6864 0000 0006 0001 0002 0001 4d54  MThd..........MT
00000010: 726b 0000 002b 0090 4040 0290 3e40 0190  rk...+..@@..>@..
00000020: 3a40 0090 4040 0190 4740 0190 4340 0090  :@..@@[email protected]@..
00000030: 4440 0190 3e40 0190 3740 0190 3740 00ff  D@..>@[email protected]@..
00000040: 2f4d 5472 6b00 0000 2f00 9040 4001 9037  /MTrk.../..@@..7
00000050: 4001 903f 4001 9040 4002 903c 4000 9044  @..?@..@@..<@..D
00000060: 4001 903e 4002 9045 4001 9046 4001 904e  @..>@[email protected]@..N
00000070: 4001 9047 4000 ff2f                      @..G@../

Since the Fugue compiler does not support voice wrapping, a direct translation of Martin Ender's Prelude answer turns out to not be the shortest possible solution. This program is equivalent to the Prelude program:

 v6(1-)#
?!^ 8- (^!)

See my Hello World answer for more information on how to use the compiler. Finally, here's a valid MIDI version of the program:

00000000: 4d54 6864 0000 0006 0001 0002 0001 4d54  MThd..........MT
00000010: 726b 0000 002c 0090 4040 0290 3e40 0190  rk...,..@@..>@..
00000020: 3a40 0090 4040 0190 4740 0190 4340 0090  :@..@@[email protected]@..
00000030: 4440 0190 3e40 0190 3740 0190 3740 00ff  D@..>@[email protected]@..
00000040: 2f00 4d54 726b 0000 0030 0090 4040 0190  /.MTrk...0..@@..
00000050: 3740 0190 3f40 0190 4040 0290 3c40 0090  7@..?@..@@..<@..
00000060: 4440 0190 3e40 0290 4540 0190 4640 0190  D@..>@[email protected]@..
00000070: 4e40 0190 4740 00ff 2f00                 [email protected]@../.
\$\endgroup\$
0
\$\begingroup\$

BRASCA, 11 bytes

x48S-:n[:n]

Explanation

<implicit input> - Take input from STDIN
x                - Clean up the linefeed from STDIN
 48S-            - Remove 48 from the digit's ascii code
     :n          - Output the number
       [:n]      - While non-zero: Output the number

Language Link

Github Repo

\$\endgroup\$
0
\$\begingroup\$

Python 3, 47 bytes

Not a winner, but I like the method.

set(iter(lambda x=input():(print(x),x)[1],'0'))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

x86_16 machine code - 20 bytes

3E A0 82 00   MOV AL, DS:[82H]
8A D0         MOV DL, AL
          _LOOP:
B4 02         MOV AH, 02H
CD 21         INT 21H
80 FA 31      CMP DL, "1"
74 F7         JZ _LOOP
             
B8 00 4C      MOV AX, 4C00H
CD 21         INT 21H

Tested in DOSBox :

Input 0

enter image description here

Input 1

If input 1 gives infinite loop

enter image description here

\$\endgroup\$
0
\$\begingroup\$

V (vim), 18 13 bytes

xqqp/1
@qq@qx

Try it online!

Somehow, there was no existing vim answer for this. I was happy to oblige.

-5 bytes from Aaron Miller.

Explanation

xqqp/1
x      delete the input char 
 qq      start macro q
   p     paste the input
    /1   find 1 (exits if 0 isn't matched)

@qq@qx    
@q       recursively call macro q
  q    end macro q
   @q  call macro q
     x delete last char (never gets executed in the case of 1)
\$\endgroup\$
5
  • \$\begingroup\$ Does this meet the "If that number is 1, print out 1 forever." requirement? Trying it online doesn't produce any output \$\endgroup\$ Commented Apr 16, 2021 at 16:17
  • \$\begingroup\$ vim doesn't show output on tio, sadly. The keystrokes work correctly in the terminal. \$\endgroup\$
    – Razetime
    Commented Apr 16, 2021 at 16:18
  • \$\begingroup\$ Maybe I can upload a gif. \$\endgroup\$
    – Razetime
    Commented Apr 16, 2021 at 16:18
  • 1
    \$\begingroup\$ @Razetime 13 bytes - /1 will work in this case instead of :s/1/1, and if you put x before the macro instead of using Y, then you can replace the dk with another x. \$\endgroup\$ Commented Apr 16, 2021 at 22:23
  • \$\begingroup\$ @AaronMiller Great, added to the answer. \$\endgroup\$
    – Razetime
    Commented Apr 17, 2021 at 2:34
0
\$\begingroup\$

Pinecone, 41 bytes

n:"".input;n="1"?(tru@print:1)|(print:0)
\$\endgroup\$
0
\$\begingroup\$

CSASM v2.3.1, 54 bytes

func main:
in ""
conv i32
.lbl a
dup
dup
print
brtrue a
ret
end

Explanation:

func main:
    ; Get the input and convert it to an integer
    in ""
    conv i32

    .lbl a
        ; Duplicate the input twice
        ; Both the "print" and "brtrue" instructions pop a value from the stack
        ;   so this is necessary to keep a 1 on the stack when looping
        dup
        dup
        print

        ; Truthy values are a non-zero integer or a non-null object
        brtrue a
    ret
end
\$\endgroup\$
0
\$\begingroup\$

CASL II, 72 bytes.

Tested here.

A START
 IN B,1
D OUT B,1
 LAD GR1,48
 XOR GR1,B
 JNZ D
 RET
B DS 1
 END

If you are familiar with assembly languages, you would get it well. It uses standard i/o. It uses ASCII-compatible encoding.

\$\endgroup\$
2
  • \$\begingroup\$ It's stupid that START requires a label. \$\endgroup\$
    – user100411
    Commented May 5, 2021 at 5:20
  • \$\begingroup\$ Also it's buggy because 1 in IN B,1 and OUT B,1 is an address rather than a literal; maybe buffer overflow may occur when assembled. However, wherever the assembled code starts from, it seemed that those two macros are assembled using LAD command: actually not problem, at least on this emulator. \$\endgroup\$
    – user100411
    Commented May 6, 2021 at 7:09
0
\$\begingroup\$

Pxem, 0 bytes (content) + 13 bytes (filename).

Filename is:

._.c.n.w1.o.a

Try it online!

As verbose pseudocode

push numeircal integer input
dup
unless empty: printf "%d", $(pop)
while one of
  1. empty
  2. $(pop) != 0
do
  push "1", a character
  unless empty: printf "%c", $(pop)
done
\$\endgroup\$
0
\$\begingroup\$

Duocentehexaquinquagesimal, 2 bytes

Try it online! Takes input as a character, and outputs in unary using characters.

\$\endgroup\$
0
\$\begingroup\$

Subleq (8-bit), 15 bytes

-1 15   3
15 16   6
15 -1 -48
16  8  -1
15  8   6

Explanation

  • 0: -1 15 3 Input character to 15:, goto 3
  • 3: 15 16 6 16: = 16: - 15: (16: = -15), goto 6
  • 6: 15 -1 -48 Output 15: (-48 is unused in instruction and used for memory)
  • 9: 16 8 -1 8: = 8: - 16: , if 8: <= 0 exit; exit for all characters "0" and before
  • 12: 15 8 6 8: = 8: - 15: (back to -48), if 8: <= 0 goto 6
\$\endgroup\$
0
\$\begingroup\$

Knight, 13 11 bytes

New method with suggestions from the language's author:

I+0P W1O1O0

Try it online!

# Read a string, coerce to number by adding 0, and check if non-zero
: IF (+ 0 PROMPT) {
    # if it was non-zero, loop printing 1 forever
    : WHILE 1 {
        : OUTPUT 1
    }
    # else output 0 and exit.
    {
        : OUTPUT 0
    }

Old version which I wrote on my own:

;=wP;W+0wO1O0

There are a few ways to do this. Decided this way because of a certain theme.

Ungolfed version:

# Read string into input
; = input PROMPT
# Coerce input to a number by adding 0, and loop while not 0
; WHILE (+ 0 input) {
    # Output 1 and a new line
    : OUTPUT 1
    # this is an infinite loop
}
# Otherwise, output 0 and a new line, and quit
: OUTPUT 0

If we allow excess newlines, this is an arguably superior program:

;=wP;W+0wOwOw

However, this prints two new lines because PROMPT includes the trailing newline. 😔

\$\endgroup\$
0
\$\begingroup\$

Barrel, 10 bytes

This makes use of the fact that the input will be only 0 or 1:

λn?a:#∞n

Explanation:

λ        // input into the accumulator
 n       // print the accumulator
  ? :    // if-else statement (auto-closes to '?a:#∞n::')
   a     // if the accumulator evaluates to truthy (i.e. non-zero)...
     #∞  // ...loop forever...
       n // ...implicitly print the accumulator...
         // ...else do nothing

In other news, I really should make a custom charmap for barrel. With a custom charmap, I could shave off 3 bytes.

\$\endgroup\$
0
\$\begingroup\$

Squire, 47 bytes

x=inquire()whence l proclaim(x+" ")if"0"!=x{l:}

Whilst some languages have a goto keyword, Squire useth whence, the opposite. One might call it a comefrom. Squire also containeth a normal loop with whilst, but alas, it is impossible to useth this without enscribing proclaim twice.

If we accompany whence with if, we can create a loop that some call do-whilst.

Ungolfeth:

# Inquire a line from ye standard input
x = inquire()
# Set a whence point
whence loop
# Proclaim x, followed by a space, to ye standard output
proclaim(x + " ")
# If x is not "0"
if "0" != x {
    # Loopeth ad infinitum
    loop:
}

Methinks this language is ridiculous. 😂

\$\endgroup\$
0
\$\begingroup\$

Nim-Lang, 72, 64, 49, 41 (credits to @hyper-neutrino and @Jo King) bytes

var a=readChar(stdin)
while a=='1':echo 1
echo 0

Version with if-statement, with suggested edits:

if readChar(stdin)=='0':echo 0
else:
  while true:echo 1
\$\endgroup\$
4
  • 1
    \$\begingroup\$ I'm not familiar with Nim, but could you remove some whitespace (e.g. after the two colons)? Also, does if'0'==a work instead? Also, is while 1 allowed? That's a trick in Python but I don't know if Nim accepts that. Finally, could you use an if-else instead, or even better, if a is 1, while loop, and then just echo 0 at the end, since the while loop will never terminate anyway? \$\endgroup\$
    – hyper-neutrino
    Commented Jun 27, 2021 at 22:32
  • 1
    \$\begingroup\$ No, while 1: is not allowed, unfortunately. However, the space can be removed. Thanks for the tip. \$\endgroup\$
    – Qaziquza
    Commented Jun 28, 2021 at 2:42
  • 1
    \$\begingroup\$ do you actually need to assign to a? can you just use readChar(stdin) in the condition? \$\endgroup\$
    – Jo King
    Commented Jun 28, 2021 at 3:37
  • \$\begingroup\$ Oh, right. Sorry, stupid of me. \$\endgroup\$
    – Qaziquza
    Commented Jun 28, 2021 at 20:07
0
\$\begingroup\$

INTERCAL, 41 bytes

Based on answer by @unrelatedstring. If you don't care for errors, won't you guys golf off the program easily?

DOWRITEIN.1DOCOMEFROM.1(1)PLEASEREADOUT.1

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Batch, 28 bytes

:1
@Echo(%1&Goto:%1
:0

uses input as output and script flow controller.

\$\endgroup\$
0
\$\begingroup\$

Add++, 7 bytes

+?
Dx,O

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ doesn't seem to work anymore but here is a 9 byte \$\endgroup\$
    – scpchicken
    Commented Jan 4, 2022 at 22:54
0
\$\begingroup\$

makina, 23 bytes

v>>P
>?EJ
U>PU
^0n<
;1<

Explanation

The first automaton starts in the top left, and immediately goes down and right into the ?, or if. The condition for the if is the E, or number input. Once input is given, the condition automaton jumps forwards and out of bounds, causing it to return its value. If that value is 0, or false, the first automaton turns left and goes into the top P, or print. The print spawns a new automaton going right, which jumps over the U-turn, goes left, into the n, or number. It reads the 0, turns up, does a U-turn, hits the semicolon (ending the number), and goes out of bounds, returning a 0 to the print. The first automaton then goes out of bounds and halts. However, if the value is 1, or true, the automaton turns right and proceeds directly into the n, goes left, reads the 1, and halts. Then, the first automaton goes out if the print, into the U-turn, making it go back to the arrow to the left of the print, starting the whole cycle again.

\$\endgroup\$
0
\$\begingroup\$

SMALL, 14 bytes

$->x{x=1?1!}0!

Ungolfed code:

$ -> x

{
    x = 1?
    1!
}

0!

We first read from stdin and store the input in x, with the following line.

$->x

We then enter an iteration, where we first compare if x is 1. If that's the case, we print 1. Since this check will never be false, this iteration will be infinite.

{
    x = 1?
    1!
}

If the condition in this iteration is false, the entire iteration stops and we go to the last line, which prints a single zero after which the program halts.

0!
\$\endgroup\$
0
\$\begingroup\$

A0A0, 67 bytes

I0S1M2V0G0
G-1G-1G-1G-1
O0

A0A0
A0C3G1G1A0
A0O1A0
A0A1G-3G-3A0
G-3

The top line takes input, then adds one to this number and multiplies it by two. For input 0 we now have 2 and for input 1 we have 4. This is used as the offset for the goto instruction. If we jump by two we end up at line three which prints zero and halts. If we jump by four, we get to line five and get to an fininite loop.

A0  A0
A0  C3 G1  G1  A0
A0  O1 A0
A0  A1 G-3 G-3 A0
G-3

This infinite loop simply prints one (on the third line in the loop: O1).

\$\endgroup\$

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.