150
24
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
or1
, byte or unary I/O is acceptable.
- If your language is incapable of outputting the characters
- when the input is
1
, it must continually print1
s 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 infinite1
s with each1
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 output1
with a newline after it all1
s 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 code-golf 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>
Can we assume that the program halts when the processor finishes executing the written code, for a machine code entry? – lirtosiast – 2015-11-03T16:58:59.960
@ThomasKwa Yes. – a spaghetto – 2015-11-03T17:25:55.860
3Assuming any behaviour is fine for all invalid inputs? – Cruncher – 2015-11-03T17:33:19.720
3@Cruncher Yes, the only inputs you should expect to get are 0 and 1. – a spaghetto – 2015-11-03T17:38:29.830
Is it alright if I just have single spaces between the
1
s? – El'endia Starman – 2015-11-04T08:54:08.247@El'endiaStarman Sure. I'll add that to the rules. – a spaghetto – 2015-11-04T15:52:15.117
4Catalog is borked. – Addison Crump – 2015-11-06T15:18:55.057
2Catalog appears to consider
Bf
andbf
to be different languages. – Mooing Duck – 2015-11-10T01:13:11.553Without having looked at all of them, I suspect a significant number of entries here to be expecting inputs of "0\n" or "1\n". I expect this was expected by @quartata but it's not necessarily explicit in the challenge. Is there a standard definition of "input a number" somewhere on meta? – Sparr – 2015-11-17T05:53:19.770
100th upvote! :) – FlipTack – 2016-12-22T11:56:15.947
Are non-zero exit codes ok? Also, can I forgo a space that would normally separate the
1
s? (i.e.11 1 1 1 1 1 1 1
) I assume not for the second question. – MildlyMilquetoast – 2017-02-02T03:15:15.503is exiting with an error allowed? – 12Me21 – 2017-02-09T00:01:15.563
@12Me21 This is allowed by default on all challenges. – a spaghetto – 2017-02-11T00:04:20.627
Is taking input as \x00 and \x01 over STDIN allowed? – CalculatorFeline – 2017-06-06T22:06:31.257
@CalculatorFeline "If your language is incapable of outputting the characters 0 or 1, byte or unary I/O is acceptable." – a spaghetto – 2017-06-07T17:02:29.650
Well, my language does allow that, so :( RIP Newbiefuck, 4 bytes:
,[.]
I/O as \x00 and \x01 – CalculatorFeline – 2017-06-07T18:48:58.377While attempting this, I made a program that does just the opposite, when given zero it prints out infinite zeroes, and when given 1, does nothing
– FantaC – 2018-01-17T02:32:06.747Can the form of
1
change—is1 1/1 1/1 1/1...
allowed? – Khuldraeseth na'Barya – 2018-03-03T20:43:57.567