It looks like we have a consensus that we want certain defaults for the format which answers are expected in for code-golf. On that poll, the question arose twice, which input/output formats should be allowed for programs and functions.
So here is another poll. This one works different though. All the input/output methods are independent of each other, so there will be one answer per method. Upvote all you think are reasonable for the default. Downvote those which you think shouldn't be allowed unless the OP explicitly permits them.
To keep this remotely manageable, I have not posted individual answers for all possible inputs for functions. So there are only four: functions can take input/output via their arguments and return values. Or functions can use any method full programs can. I don't think there is any point in (say) allowing programs to take input from STDIN (only) and to allow functions to take input from ARGV (only) or something like that. If you disagree, please leave a comment.
If I've overlooked an I/O method, feel free to add your own answer.
Note: Some votes have been reverted because they were detected as serial voting. If you vote on multiple answers, please leave some time between votes.
A method is allowed if it has 5 net votes and at least twice as many upvotes as downvotes.
-
\$\begingroup\$ Related (Can numeric input/output be in the form of byte values) \$\endgroup\$mbomb007– mbomb0072016年12月13日 16:10:17 +00:00Commented Dec 13, 2016 at 16:10
-
6\$\begingroup\$ "The current results of the polls are now part of the tag wiki" which tag wiki? meta wiki has nothing for code-golf tag. ppcg wiki has codegolf.stackexchange.com/tags/code-golf/info but it's missing a lot of answers here. \$\endgroup\$Sparr– Sparr2018年07月18日 18:17:05 +00:00Commented Jul 18, 2018 at 18:17
119 Answers 119
Functions may output via their return value(s)
-
1\$\begingroup\$ How many languages support this? I support outputting to STDOUT. \$\endgroup\$user10766– user107662014年11月03日 00:16:24 +00:00Commented Nov 3, 2014 at 0:16
-
36\$\begingroup\$ @hosch250 Wait, are there languages that have functions without return values? o.O \$\endgroup\$Martin Ender– Martin Ender2014年11月03日 00:17:15 +00:00Commented Nov 3, 2014 at 0:17
-
\$\begingroup\$ Oh, that is not considered outputting the last I heard - that is returning a value. If the question asks for a function, this is correct. If the question asks for a program, I support outputting to STDOUT or a file. \$\endgroup\$user10766– user107662014年11月03日 00:19:18 +00:00Commented Nov 3, 2014 at 0:19
-
4\$\begingroup\$ @hosch250 This particular answer only concerns functions. Whether functions and/or programs are allowed is another question (see the linked meta question, but of course it's ultimately up to the challenge author). But this answers says that if functions are allowed, then they should be allowed to use their return value for the result - as are all other answers here that start with the word
Functions
. \$\endgroup\$Martin Ender– Martin Ender2014年11月03日 00:21:12 +00:00Commented Nov 3, 2014 at 0:21 -
\$\begingroup\$ Oh, sorry. I definitely will vote on this then. \$\endgroup\$user10766– user107662014年11月03日 00:22:09 +00:00Commented Nov 3, 2014 at 0:22
-
1\$\begingroup\$ @MartinEnder Most (all?) 8-bit BASIC interpreters do not specifically return a value from a sub-routine, whilst many will allow you to define a mathematical function with the
DEF
keyword, this is quite limited. I also don't recall Amiga BASIC included with WorkBench having return values from sub-routines but it's been many years since I did any Amiga BASIC so don't quote me on that. 6502/10 assembly does not return values from sub-routines either. If you know that you've stored your result in the accumulator, then you simply use that on return from your sub-routine. \$\endgroup\$Shaun Bebbers– Shaun Bebbers2017年04月04日 08:58:12 +00:00Commented Apr 4, 2017 at 8:58 -
2\$\begingroup\$ @MartinEnder Yes, scratch has no return values for it's functions \$\endgroup\$The Fifth Marshal– The Fifth Marshal2017年08月27日 15:51:59 +00:00Commented Aug 27, 2017 at 15:51
-
1\$\begingroup\$ @Hosch250 There are languages that don't have STDOUT, like JS \$\endgroup\$Luvexina– Luvexina2019年07月03日 20:39:40 +00:00Commented Jul 3, 2019 at 20:39
-
1\$\begingroup\$ @VFDan In case of JS,
console.log
-ing is often regerded as STDOUT. Also, Node.js does have a STDOUT. \$\endgroup\$FZs– FZs2021年05月03日 06:43:39 +00:00Commented May 3, 2021 at 6:43
Functions may take input via function arguments
-
2\$\begingroup\$ I use this in a lot of my questions, so definitely. \$\endgroup\$Joe Z.– Joe Z.2014年11月16日 22:43:59 +00:00Commented Nov 16, 2014 at 22:43
-
2\$\begingroup\$ Can it take array via repeatedly call a function, once a value? \$\endgroup\$l4m2– l4m22017年12月18日 21:39:08 +00:00Commented Dec 18, 2017 at 21:39
-
3\$\begingroup\$ @l4m2 that sounds like a special form of currying and would be worth asking about in a separate question. (In principle, this should be valid, but it's worth discussing explicitly.) \$\endgroup\$2017年12月18日 21:40:55 +00:00Commented Dec 18, 2017 at 21:40
Programs may take input via STDIN
Programs may output to STDOUT
Programs may take input via GUI prompts
(This is for languages, for which this is the closest alternative to STDIN, like JavaScript's prompt()
, Mathematica's Input[]
or InputString[]
, Matlab's input()
and VBScript's InputBox()
.)
-
1\$\begingroup\$ What about for other languages? \$\endgroup\$Ypnypn– Ypnypn2014年11月04日 00:32:05 +00:00Commented Nov 4, 2014 at 0:32
-
\$\begingroup\$ @Ypnypn I'm not aware of a language which can read from STDIN, but where producing a graphical prompt is shorter than that. But in that case, I'd say either is fine. \$\endgroup\$Martin Ender– Martin Ender2014年11月04日 09:22:32 +00:00Commented Nov 4, 2014 at 9:22
-
1\$\begingroup\$ @MartinBüttner Matlabs
input('')
usually just reads from the command line, perhaps you meaninputdlg('')
? \$\endgroup\$flawr– flawr2015年09月11日 16:29:36 +00:00Commented Sep 11, 2015 at 16:29 -
\$\begingroup\$ Anyway, Mma's
Input
andInputString
both take the next line of input when run in a script. \$\endgroup\$LegionMammal978– LegionMammal9782015年11月08日 12:35:12 +00:00Commented Nov 8, 2015 at 12:35 -
\$\begingroup\$ @MartinEnder
wscript
. \$\endgroup\$wizzwizz4– wizzwizz42016年07月21日 15:47:44 +00:00Commented Jul 21, 2016 at 15:47 -
1\$\begingroup\$ Bash
read <var>
\$\endgroup\$Stan Strum– Stan Strum2018年01月07日 22:09:49 +00:00Commented Jan 7, 2018 at 22:09 -
1\$\begingroup\$ So reading choice from
confirm
andmsgbox
etc. are also allowed \$\endgroup\$l4m2– l4m22018年03月10日 08:56:57 +00:00Commented Mar 10, 2018 at 8:56
Programs may output by displaying it on screen.
This makes it possible to use languages like Vim script that can't print output directly to stdout.
Example from this challenge:
$ echo "This is a test line!" | vim - -c 'nm Q vEUWvEuWQ|norm Q'
Will display:
THIS is A test LINE!
~
~
~
~
~
Within a Vim session.
I think that this way of displaying the result should be valid. I am sure that there are other domain specific languages like Vim script that lacks full I/O support and could use another way to display the result.
-
4\$\begingroup\$ Incidentally, vim's output is standard output, but if you processed standard output, there is extra garbage there. \$\endgroup\$Joshua– Joshua2016年01月25日 20:04:41 +00:00Commented Jan 25, 2016 at 20:04
-
3\$\begingroup\$ It's not even just domain specific languages. I'd say QBasic falls in this category: it doesn't have any concept of an output (or input) stream, but rather writes characters to a "window." \$\endgroup\$DLosc– DLosc2016年09月03日 03:51:23 +00:00Commented Sep 3, 2016 at 3:51
Programs may output using their exit code...
Exit codes are basically a return value for programs. If functions can output using their return values, it makes sense that programs should be able to do the same.
Examples:
-
\$\begingroup\$ Hmm...should this perhaps be "if and only if the challenge requires as output an integer whose value is guaranteed to be between 0 and 255, inclusive"? For example, if the challenge is "output the number of arguments passed to the program," then
int main(int argc, char **argv) { return argc; }
should not be a valid solution (IMO), because the exit code is masked by0xFF
. (Perhaps this is implicit in the rule as a part of correctness? Just want to be clear.) \$\endgroup\$wchargin– wchargin2016年12月18日 05:05:10 +00:00Commented Dec 18, 2016 at 5:05 -
6\$\begingroup\$ @wchargin That seems like a limitation of whatever you are using to run your program. I can write a PowerShell script like
exit 1000000
, and execute it in PowerShell ISE. This gives me no problems. \$\endgroup\$Rainbolt– Rainbolt2016年12月19日 19:14:59 +00:00Commented Dec 19, 2016 at 19:14 -
3\$\begingroup\$ Interesting—I didn't know that. As far as I know, this is true on all *nix OSes (including Mac), but I'd be happy to be corrected! \$\endgroup\$wchargin– wchargin2016年12月19日 19:21:31 +00:00Commented Dec 19, 2016 at 19:21
-
\$\begingroup\$ is there a reason to limit this to integers? i mean if there's an environment where a program can return something other than an integer why not allow that too? \$\endgroup\$Jasen– Jasen2016年12月22日 10:21:35 +00:00Commented Dec 22, 2016 at 10:21
-
\$\begingroup\$ @Jasen I don't see a reason to limit it to integers. \$\endgroup\$Rainbolt– Rainbolt2016年12月22日 14:04:28 +00:00Commented Dec 22, 2016 at 14:04
-
\$\begingroup\$ I'm new to this site. Would a crashed program's exit code considered a valid way of output (for example,
SIGSEGV
)? \$\endgroup\$Shmuel H.– Shmuel H.2017年01月09日 16:04:25 +00:00Commented Jan 9, 2017 at 16:04 -
6\$\begingroup\$ @ShmuelH. If you intentionally crash a program (or just use the equivalent of
exit(code)
in that language) to get a specific exit code, it's valid. \$\endgroup\$user45941– user459412017年01月09日 16:31:41 +00:00Commented Jan 9, 2017 at 16:31
Programs may take input via command-line arguments
The contents of the tape post-execution may be used as a Turing machine's output
-
7\$\begingroup\$ This has my upvote. \$\endgroup\$SuperJedi224– SuperJedi2242015年09月22日 20:54:14 +00:00Commented Sep 22, 2015 at 20:54
-
6\$\begingroup\$ Emphasis on the "post-execution". If you allow the final tape of a non-halting turing machine to be considered out, your program can calculate a non-computable function. \$\endgroup\$Christopher King– Christopher King2018年12月10日 14:15:52 +00:00Commented Dec 10, 2018 at 14:15
Functions may take multiple arguments via currying
For some functional programming languages like Haskell this is actually necessary, because only single-argument functions exist and functions with multiple arguments are (somewhat transparently) implemented as curried functions. (The alternative would be to take a list or tuple of the values, but that is not how one would naturally write a two-argument function in Haskell.)
As per this consensus the same should also be allowable for languages where multi-argument functions do exist. As an example from JavaScript, instead of defining
f=(a,b)=>...
and calling it like f(a,b)
one could then also define
f=a=>b=>...
and call it like f(a)(b)
.
-
\$\begingroup\$ Isn't that how lambda calculus works? \$\endgroup\$Lucenaposition– Lucenaposition2024年08月13日 11:09:09 +00:00Commented Aug 13, 2024 at 11:09
-
\$\begingroup\$ @Lucenaposition Yep, that's one of the reasons why it's allowed \$\endgroup\$Weird Glyphs– Weird Glyphs2025年02月05日 20:31:33 +00:00Commented Feb 5 at 20:31
Functions may output via the same methods as full programs
(This depends on how the poll goes, but could be any subset of STDOUT, STDERR and file.)
-
\$\begingroup\$ This violates the single-responsibility principle, the separation of concerns principle, and I don't think it's really useful in code golf. Moreover, functional programming languages won't let you do that at all, and I want some more love for Haskell. \$\endgroup\$John Dvorak– John Dvorak2014年11月03日 06:42:03 +00:00Commented Nov 3, 2014 at 6:42
-
4\$\begingroup\$ @JanDvorak One simple golf use is that
print
is shorter thanreturn
in Python. \$\endgroup\$xnor– xnor2014年11月03日 08:57:35 +00:00Commented Nov 3, 2014 at 8:57 -
5\$\begingroup\$ Eugh. Not a fan of that optimisation. What it could be useful for is printing in a loop rather than collecting results in an array. Still not a fan of allowing that. \$\endgroup\$John Dvorak– John Dvorak2014年11月03日 09:02:21 +00:00Commented Nov 3, 2014 at 9:02
-
\$\begingroup\$ @JanDvorak Yeesh, I did not consider that one could print repeatedly to "output" a list. I think that should not be allowed even if this vote passes because the output is not the list that is asked for. \$\endgroup\$xnor– xnor2014年11月03日 09:06:03 +00:00Commented Nov 3, 2014 at 9:06
-
12\$\begingroup\$ @xnor Why not, full programs are allowed to do that, too, right? \$\endgroup\$Martin Ender– Martin Ender2014年11月03日 11:22:17 +00:00Commented Nov 3, 2014 at 11:22
-
\$\begingroup\$ @MartinBüttner Hmm, you're right, what a program really "outputs" is the string representation of the result, so this rule should mean functions can too for consistency. But this is another awkward consequence that makes me disfavor this rule. \$\endgroup\$xnor– xnor2014年11月04日 02:15:06 +00:00Commented Nov 4, 2014 at 2:15
-
\$\begingroup\$ what about exit codes? aren't functions suposed to be reusable? if my function exits the program it isn't reusable. that's what the title indicates \$\endgroup\$Felipe Nardi Batista– Felipe Nardi Batista2017年10月26日 13:14:24 +00:00Commented Oct 26, 2017 at 13:14
Programs may output to STDERR
-
11\$\begingroup\$ Can anyone give a comment as to why not to? For example,
print("Hello, world!")
in lua prints to STDERR. \$\endgroup\$yyny– yyny2016年01月31日 00:40:16 +00:00Commented Jan 31, 2016 at 0:40 -
7\$\begingroup\$ Forbidding output to STDERR seems incompatible with allowing output to the screen. \$\endgroup\$2016年05月02日 18:47:20 +00:00Commented May 2, 2016 at 18:47
-
1\$\begingroup\$ This should go as standar... Ah. I see. \$\endgroup\$wizzwizz4– wizzwizz42016年07月21日 16:00:43 +00:00Commented Jul 21, 2016 at 16:00
-
\$\begingroup\$ The only problem I see with this is vagueness between what is written to STDERR by the "language" and what is written by the "program." If a distinction between those is not made, this would permit error quines and answers like this, which is questionably valid. \$\endgroup\$Esolanging Fruit– Esolanging Fruit2016年12月29日 18:29:59 +00:00Commented Dec 29, 2016 at 18:29
-
\$\begingroup\$ I think this shouldn't be allowed by default (but could be explicitly allowed on some challenges). I believe this goes against the spirit of some challenges such as this. On the other hand it could be explicitly forbidden but not everybody will think of this \$\endgroup\$drolex– drolex2017年01月27日 12:36:04 +00:00Commented Jan 27, 2017 at 12:36
-
9\$\begingroup\$ This is game-breaking for PHP, where
<?=a;
outputsa
to STDOUT andNotice: Use of undefined constant a - assumed 'a' in [...][...] on line 3
to STDERR. \$\endgroup\$Ismael Miguel– Ismael Miguel2017年01月27日 14:35:02 +00:00Commented Jan 27, 2017 at 14:35 -
6\$\begingroup\$ @IsmaelMiguel This means that outputting to STDERR only counts if that's your chosen output format. \$\endgroup\$MilkyWay90– MilkyWay902019年04月06日 22:31:47 +00:00Commented Apr 6, 2019 at 22:31
-
2\$\begingroup\$ @YoYoYonnY Since when? All Lua 5.1-5.3 output to stdout... \$\endgroup\$val - disappointed in SE– val - disappointed in SE2019年07月17日 14:16:39 +00:00Commented Jul 17, 2019 at 14:16
Functions may output by modifying their arguments or writing to out arguments
Here, "out arguments" refers to arguments that are passed in by reference, so that modifying the value inside the function (as opposed to just overwriting the argument) also modifies the value outside the function (otherwise, the output value would not be observable after the function returns). Note that this is not possible in all languages with all types.
-
3\$\begingroup\$ Does this mean that in languages that allow it, the seven characters for
return
can simply be replaced with an assignment? \$\endgroup\$lirtosiast– lirtosiast2015年06月10日 21:05:02 +00:00Commented Jun 10, 2015 at 21:05 -
9\$\begingroup\$ @ThomasKwa Yes, provided the changed value will then be accessible in the context that called the function. \$\endgroup\$2015年06月10日 21:18:06 +00:00Commented Jun 10, 2015 at 21:18
-
2\$\begingroup\$ so you mean this is only legal in languages that have out arguments, pass by reference, or some similar semantic? for instance not in shell... \$\endgroup\$Jasen– Jasen2016年12月22日 10:27:23 +00:00Commented Dec 22, 2016 at 10:27
-
\$\begingroup\$ @Jasen I don't know about shell, but yes to your question. \$\endgroup\$2016年12月22日 10:31:03 +00:00Commented Dec 22, 2016 at 10:31
-
-
\$\begingroup\$ @isaacg you need to link to a specific revision for that comment to make sense. \$\endgroup\$Peter Taylor– Peter Taylor2017年06月30日 19:58:54 +00:00Commented Jun 30, 2017 at 19:58
-
1\$\begingroup\$ The revision that I referred to about is codegolf.stackexchange.com/revisions/128782/6 \$\endgroup\$izzyg– izzyg2017年06月30日 23:35:04 +00:00Commented Jun 30, 2017 at 23:35
For stack-based languages, function's input may be pushed to the stack before calling
Stack-based languages may assume that the input for their function is automatically pushed to the stack.
-
1\$\begingroup\$ +1, this is the de facto standard for CJam and GolfScript (and probably some other stack-based languages). You might want to make a corresponding answer for output. \$\endgroup\$2016年02月19日 13:29:08 +00:00Commented Feb 19, 2016 at 13:29
-
7\$\begingroup\$ I think the emphasis here needs to be on "function", as opposed to snippet. There's a very fine line between the two... \$\endgroup\$Sp3000– Sp30002016年02月19日 13:34:59 +00:00Commented Feb 19, 2016 at 13:34
-
1\$\begingroup\$ Are there any stack based langauges that let you define functions? As far as I am aware most of them do not and do only let you write full programs with IO via STDIO. (in my opinion functions should satisfy (perhaps among other criterions): pieces of code that can be reused in the same program, without typing them out again) \$\endgroup\$flawr– flawr2016年07月10日 16:27:07 +00:00Commented Jul 10, 2016 at 16:27
-
\$\begingroup\$ @flawr Vitsy has functions, in some aspects. There are a few stack-based languages that have scope, but I can't name them immediately. \$\endgroup\$Addison Crump– Addison Crump2016年07月11日 04:12:11 +00:00Commented Jul 11, 2016 at 4:12
-
4\$\begingroup\$ @flawr for completion sake: lots (most?) of non-esoteric stack-based languages let you define functions. Factor, Forth, Postscript, Joy, Cat, and many others all have functions (both named and anonymous). dc (the RPN calculator) has registers that can work like functions. \$\endgroup\$fede s.– fede s.2016年09月14日 17:35:22 +00:00Commented Sep 14, 2016 at 17:35
-
\$\begingroup\$ @flawr: Underload lets you reuse the functions in question (in a kind-of awkward way, as Underload has no method of naming things, but it's possible and fairly common). \$\endgroup\$user62131– user621312017年01月15日 17:05:32 +00:00Commented Jan 15, 2017 at 17:05
-
1\$\begingroup\$ Would this apply to a language that has explicit but no implicit IO? \$\endgroup\$Adalynn– Adalynn2017年08月05日 17:19:52 +00:00Commented Aug 5, 2017 at 17:19
Programs may output to a file
-
12\$\begingroup\$ Why are people against this? \$\endgroup\$xnor– xnor2014年11月08日 05:48:57 +00:00Commented Nov 8, 2014 at 5:48
-
8\$\begingroup\$ @xnor is
/dev/null
considered a valid output file? I might as well submit a blank program and claim it outputs the correct answer to/dev/null
. \$\endgroup\$Patrick Roberts– Patrick Roberts2016年02月15日 21:55:06 +00:00Commented Feb 15, 2016 at 21:55 -
16\$\begingroup\$ @PatrickRoberts No,
/dev/null
is not a valid output file, for the exact reason that the output cannot be examined later. "Output to a file" means that you can see the program's output by viewing a particular file. \$\endgroup\$DLosc– DLosc2016年09月03日 04:05:57 +00:00Commented Sep 3, 2016 at 4:05 -
19\$\begingroup\$ using strace (or chroot, or mv!) you can see what was written to /dev/null \$\endgroup\$Jasen– Jasen2016年12月22日 10:30:31 +00:00Commented Dec 22, 2016 at 10:30
-
5\$\begingroup\$ @PatrickRoberts But it doesn't output to
/dev/null
\$\endgroup\$MilkyWay90– MilkyWay902019年04月06日 22:28:30 +00:00Commented Apr 6, 2019 at 22:28
Functions may take input via the same methods as full programs
(This depends on how the poll goes, but could be any subset of STDIN, ARGV and file.)
-
\$\begingroup\$ This violates the single-responsibility principle, the separation of concerns principle, and I don't think it's really useful in code golf. Moreover, functional programming languages won't let you do that at all, and I want some more love for Haskell. \$\endgroup\$John Dvorak– John Dvorak2014年11月03日 06:43:31 +00:00Commented Nov 3, 2014 at 6:43
-
24\$\begingroup\$ @JanDvorak I don't see the relevance of good coding practices for code golf. And some languages don't even have functions and they aren't necessarily at a disadvantage by allowing functions. The idea of being more liberal in the options for I/O is so that each language can pick what's really shortest. \$\endgroup\$Martin Ender– Martin Ender2014年11月03日 11:10:34 +00:00Commented Nov 3, 2014 at 11:10
-
2\$\begingroup\$ The only time that I would object to a function taking input in the same way as a program is if the question was phrased "Write a function that takes the following arguments". This poll applies to questions that are not in such a specific format, so I see no reason to exclude functions that read from stdin. \$\endgroup\$trichoplax is on Codidact now– trichoplax is on Codidact now2014年11月04日 13:16:15 +00:00Commented Nov 4, 2014 at 13:16
-
\$\begingroup\$ @MartinBüttner noted. I still don't like this option, but feel free to override my preferences. The vote split is very close to the threshold ATM. \$\endgroup\$John Dvorak– John Dvorak2014年11月07日 09:47:20 +00:00Commented Nov 7, 2014 at 9:47
Programs may combine two or more input methods
For example, if the inputs are a string and an int, a function that takes a string as an argument and an int from STDIN would be valid.
The input format must still be consistent for a given program.
Input may be entirely ignored
If a challenge specifies that your program should take a certain selection of inputs, but not all of them are necessary for you to solve the task, you can simply not take the ones you don't need. (This is equivalent to taking them via a method that your language doesn't look at, such as via command-line argument in a language which has no access to the command line arguments.)
-
\$\begingroup\$ Are you implying that if the challenge was "take three numbers as input a, b, and c, and return a+b", then
print(input()+input())
would be a valid Python submission? \$\endgroup\$Esolanging Fruit– Esolanging Fruit2017年05月23日 03:20:59 +00:00Commented May 23, 2017 at 3:20 -
7\$\begingroup\$ @Challenger5: Right. Also that that would be a valid submission if the request was to return a+c. (Even without this, you could still do that by saying, for example, "a and c are input via standard input, and b from a file". This rule's basically meant to avoid the absurdity of having to do that.) \$\endgroup\$user62131– user621312017年05月23日 03:44:33 +00:00Commented May 23, 2017 at 3:44
Where applicable, Turing machines supporting multiple halt states may also output via their halt state
This is essentially equivalent to programs outputting via their exit code.
-
4\$\begingroup\$ This has my upvote. \$\endgroup\$SuperJedi224– SuperJedi2242015年09月22日 20:54:20 +00:00Commented Sep 22, 2015 at 20:54
-
\$\begingroup\$ Turing Machines as I've seen them defined leave output on their tape and have a single halt state. Is there some precedent for what you are suggesting? \$\endgroup\$xnor– xnor2015年09月24日 16:51:33 +00:00Commented Sep 24, 2015 at 16:51
-
\$\begingroup\$ @xnor: That is usually the case, but I have seen dialects of turing machine code that do support multiple halt states. (For example, in the morphett.info dialect that I usually use,
halt
is the basic halt state, but any state of the formhalt-<any string>
is also treated as a halt state.) \$\endgroup\$SuperJedi224– SuperJedi2242015年09月24日 19:09:31 +00:00Commented Sep 24, 2015 at 19:09
Input for Turing machines may be written to the tape pre-execution
The read-write head should start on the leftmost cell of the portion of the tape containing the input.
-
3\$\begingroup\$ This has my upvote. \$\endgroup\$SuperJedi224– SuperJedi2242015年09月22日 20:53:51 +00:00Commented Sep 22, 2015 at 20:53
-
6\$\begingroup\$ I think this should only be allowed if that's the only method of input. \$\endgroup\$mbomb007– mbomb0072015年10月08日 20:39:44 +00:00Commented Oct 8, 2015 at 20:39
-
3\$\begingroup\$ For standard turing machines, it is. \$\endgroup\$SuperJedi224– SuperJedi2242015年10月08日 20:59:17 +00:00Commented Oct 8, 2015 at 20:59
-
4\$\begingroup\$ What happened here, SuperJedi? You posted an answer, then commented "This has my upvote" an hour later? \$\endgroup\$MD XF– MD XF2017年10月31日 16:55:41 +00:00Commented Oct 31, 2017 at 16:55
-
3\$\begingroup\$ @MDXF At the time, several other answers on this question had comments like that added by their authors (including many of Martin's answers). At least one was instead commented "This has my downvote." Basically, it was intended as a way to vote on your own meta proposals because you can't actually upvote or downvote your own answers on this site. Interestingly, most of those comments on this topic seem to have since been removed. \$\endgroup\$SuperJedi224– SuperJedi2242017年11月01日 11:22:03 +00:00Commented Nov 1, 2017 at 11:22
-
2\$\begingroup\$ @SuperJedi224 Ah, okay, thanks. I was suspecting sock action, but that makes sense. \$\endgroup\$MD XF– MD XF2017年11月02日 16:37:12 +00:00Commented Nov 2, 2017 at 16:37
Functions may return a list via acting as a generator
In other words, writing a function to return list elements lazily on request, using support that the language already has for doing that, is allowed (currently it's frequently seen but something of a gray area). This would be idiomatic in most of the languages which have it.
-
2\$\begingroup\$ I wasn't aware this was even a point of contention... \$\endgroup\$user45941– user459412016年11月30日 21:57:24 +00:00Commented Nov 30, 2016 at 21:57
-
1\$\begingroup\$ I don't think it is, but we don't have it listed, and converting a generator to a list is fairly expensive byte-wise in Prolog (there's a builtin for it but it has a fairly long name). So I wanted to confirm that I don't have to do the conversion in my Prolog answers. \$\endgroup\$user62131– user621312016年11月30日 23:03:12 +00:00Commented Nov 30, 2016 at 23:03
-
\$\begingroup\$ Some things in Python 2 return a list, but in Python 3 it return a generator. Like
range
. \$\endgroup\$mbomb007– mbomb0072017年03月03日 22:46:22 +00:00Commented Mar 3, 2017 at 22:46 -
\$\begingroup\$ @mbomb007 I think Python 3s
range
is equivalent to Python 2sxrange
and Python 2srange
is something different... \$\endgroup\$Linnea Gräf– Linnea Gräf2017年08月27日 18:44:56 +00:00Commented Aug 27, 2017 at 18:44 -
\$\begingroup\$ Basically. Python 2's
range
is like callinglist()
on the result. \$\endgroup\$mbomb007– mbomb0072017年08月27日 19:24:11 +00:00Commented Aug 27, 2017 at 19:24 -
\$\begingroup\$ Maybe not in fastest code... \$\endgroup\$The Empty String Photographer– The Empty String Photographer2023年08月17日 16:33:12 +00:00Commented Aug 17, 2023 at 16:33
SQL submissions may take input from a named table
which is probably not good enough. But I don't know a better way.
-
\$\begingroup\$ Should be OK, as long as that input is treated as data, not executable code \$\endgroup\$roblogic– roblogic2019年08月12日 09:03:50 +00:00Commented Aug 12, 2019 at 9:03
Assembly programs may take input from registers
If there are no I/O devices available, an answer might consist of a subroutine that reads its input values from the machine registers.
-
\$\begingroup\$ There's potential for abuse. What if someone creates a machine with a million different special-purpose registers? \$\endgroup\$lirtosiast– lirtosiast2016年03月15日 17:10:18 +00:00Commented Mar 15, 2016 at 17:10
-
8\$\begingroup\$ @lirtosiast Good for code colf then. I don't see how that's different from golfing languages with prefilled vars \$\endgroup\$cat– cat2016年07月06日 00:31:20 +00:00Commented Jul 6, 2016 at 0:31
-
2\$\begingroup\$ @lirtosiast I believe the programming language used has to be defined before the challenge starts. \$\endgroup\$yyny– yyny2016年12月31日 21:19:54 +00:00Commented Dec 31, 2016 at 21:19
-
4\$\begingroup\$ If you golf with x86-64, the standard calling conventions already pass args in registers. So you can just write a normal function that's callable from compiler-generated code. \$\endgroup\$Peter Cordes– Peter Cordes2017年01月01日 07:06:28 +00:00Commented Jan 1, 2017 at 7:06
-
4\$\begingroup\$ This needs clarification: can we use any register, or only the ones that would be specified to be used by the ABI? \$\endgroup\$user62131– user621312017年06月11日 04:14:48 +00:00Commented Jun 11, 2017 at 4:14
-
\$\begingroup\$ Update on calling conventions: you don't have to use a standard C calling convention. Your language is assembly / machine code, so presumably that's the caller's language as well. They can pass args and take return value(s) in any registers including FLAGS on machines with condition codes, as discussed in Tips for golfing in x86/x64 machine code \$\endgroup\$Peter Cordes– Peter Cordes2023年07月21日 11:39:43 +00:00Commented Jul 21, 2023 at 11:39
Programs may take input as the value of the last expression
Using something like TI-84 BASIC's Ans
, which is a variable that stores the value of the last expression. This the shortest way for TI-BASIC to take input, but its validity was recently questioned. To be clear, this is how the process works with Ans
:
Within a program, type the input (number, list, string, etc.), followed by a newline to separate statements, followed by the program name, then press Enter. For example,
1337 prgmFACTOR
Outside a program, type the input, press Enter, then type the program name and press Enter.
Edit: I came across a way of using Ans
that is similar to command-line arguments, which are allowed: All as one expression, type the input, followed by a colon, followed by pgrmNAMEHERE
. For example, 1337:pgrmFACTOR
.
-
4\$\begingroup\$ I think by definition that submission would not be a full program but a snippet that requires some sort of REPL environment which are disallowed by default. \$\endgroup\$2016年02月25日 22:53:32 +00:00Commented Feb 25, 2016 at 22:53
-
9\$\begingroup\$ I think that TI-BASIC is a special case, it being fundamentally different from other languages, and only being callable from what can be considered a REPL; so what then is to differ a full program from a snippet? \$\endgroup\$Conor O'Brien– Conor O'Brien2016年02月29日 17:06:14 +00:00Commented Feb 29, 2016 at 17:06
-
4\$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ I disagree, you can write full programs in the REPL but you can also write them in separate "files" that can be executed. They even support arguments when the program is called, but can also read from stdin or via prompt. \$\endgroup\$flawr– flawr2016年03月05日 12:55:59 +00:00Commented Mar 5, 2016 at 12:55
-
3\$\begingroup\$ @flawr On the 84+ series they can't take arguments, and there's no STDIN, only a prompt. \$\endgroup\$lirtosiast– lirtosiast2016年03月15日 16:48:08 +00:00Commented Mar 15, 2016 at 16:48
-
4\$\begingroup\$ @lirtosiast According to the official TI 84+ guidebook both commands
Input
andPrompt
are available. Both let the program fetch user input. Both behave alot like e.g.input()
in Python or MATLAB. \$\endgroup\$flawr– flawr2016年03月15日 17:59:25 +00:00Commented Mar 15, 2016 at 17:59 -
1\$\begingroup\$ @flawr I agree about their similarity; what I meant is that they display a
?
that prompts the user to type something. \$\endgroup\$lirtosiast– lirtosiast2016年03月15日 18:00:49 +00:00Commented Mar 15, 2016 at 18:00 -
1\$\begingroup\$ @lirtosiast Is that an issue? I think many languages do have symbols to denote new inputs/new lines. \$\endgroup\$flawr– flawr2016年03月15日 18:03:27 +00:00Commented Mar 15, 2016 at 18:03
-
1\$\begingroup\$ @flawr It's not an issue. \$\endgroup\$lirtosiast– lirtosiast2016年03月15日 18:04:03 +00:00Commented Mar 15, 2016 at 18:04
-
4\$\begingroup\$ @flawr It's worth noting that you can't write full programs in the REPL, at least for the TI-83 and -84 calculators. For example, the
Input
/Prompt
and looping commands can't be used from the REPL. \$\endgroup\$Jakob– Jakob2017年01月24日 16:41:28 +00:00Commented Jan 24, 2017 at 16:41 -
3\$\begingroup\$ Does this mean using
_
for the Python REPL is valid? \$\endgroup\$Esolanging Fruit– Esolanging Fruit2017年02月09日 08:02:07 +00:00Commented Feb 9, 2017 at 8:02 -
2\$\begingroup\$ @JakobCornell There are actually assembly programs that let you bypass that \$\endgroup\$SuperJedi224– SuperJedi2242017年03月27日 13:46:03 +00:00Commented Mar 27, 2017 at 13:46
Functions may return a boolean value via the presence or absence of an error/exception
That is, crashing to return false
, and not crashing to return true
. This is the function equivalent of outputting via exit code, which is allowed for full programs (nearly all interpreters will return an exit code of 0 by default if the program doesn't crash, and nonzero if it does).
I realised this wasn't on the list while writing a post about how our I/O defaults applied to Brachylog; this method of outputting booleans is idiomatic in most declarative languages (i.e. it's something I'd be likely to write even if I weren't golfing), and it's still meaningful in other paradigms (much rarer, but it's nonetheless considered an acceptable programming technique in industry in languages like OCaml).
I'm not completely convinced either way about whether this should be accepted, although I'm currently mildly in favour (the main point in favour is that it's often the most natural way to do output even if you aren't golfing, and many languages have a try
/catch
or equivalent to convert it into a regular Boolean; the main point against is that it feels a bit cheap/abusable). However, we could definitely do with a ruling as to whether this is acceptable or not, as there are many cases in which it's going to be the shortest way to write the code, and thus it matters for how code-golf answers are written.
-
10\$\begingroup\$ This is pretty much "output via exit code" for functions. +1 from me. \$\endgroup\$AdmBorkBork– AdmBorkBork2017年03月29日 17:42:16 +00:00Commented Mar 29, 2017 at 17:42
-
8\$\begingroup\$ Not sure about this either way, but it would be a significant change for Python golfing. Especially so if a recursive function that keeps nesting is considered a stack overflow error, which is a bit weird since it's common to assume there's unlimited stack depth. \$\endgroup\$xnor– xnor2017年03月30日 08:58:21 +00:00Commented Mar 30, 2017 at 8:58
-
\$\begingroup\$ If anything, I think this should only be allowed in languages with some way of handling errors (usually try/catch). Otherwise, it would be impossible to actually use the code in a program. \$\endgroup\$12Me21– 12Me212018年03月02日 02:20:04 +00:00Commented Mar 2, 2018 at 2:20
-
\$\begingroup\$ @12Me21 I read "crash" as different to "error" or "exception", where "crash" is always non-recoverable. \$\endgroup\$Οurous– Οurous2018年07月30日 21:03:10 +00:00Commented Jul 30, 2018 at 21:03
-
2\$\begingroup\$ Is there a contradiction here with our stance on function reusability, namely Do function submissions have to be reusable? and Should submissions be allowed to exit with an error? \$\endgroup\$Dingus– Dingus2021年10月19日 10:57:25 +00:00Commented Oct 19, 2021 at 10:57
-
\$\begingroup\$ More recent meta discussion that expands on my comment above: Functions throwing exceptions: an exception to the rules? \$\endgroup\$Dingus– Dingus2022年08月30日 13:28:33 +00:00Commented Aug 30, 2022 at 13:28
Functions in stack-based languages can leave the output on the stack
Mostly for completeness sake. This is the de-facto standard for CJam and GolfScript and probably other stack-based languages.
-
\$\begingroup\$ This is also how Forth works. \$\endgroup\$mbomb007– mbomb0072016年08月03日 15:18:51 +00:00Commented Aug 3, 2016 at 15:18
-
\$\begingroup\$ Would this apply to a language that has explicit but no implicit IO? \$\endgroup\$Adalynn– Adalynn2017年08月05日 17:19:50 +00:00Commented Aug 5, 2017 at 17:19
-
\$\begingroup\$ @Zacharý not sure what you mean? \$\endgroup\$2017年08月05日 17:21:48 +00:00Commented Aug 5, 2017 at 17:21
-
\$\begingroup\$ A language like Befunge or something similar. Or am I misunderstanding the concept of "function" in these kinds of languages? \$\endgroup\$Adalynn– Adalynn2017年08月05日 17:28:23 +00:00Commented Aug 5, 2017 at 17:28
-
\$\begingroup\$ @Zacharý Befunge doesn't have functions so this answer doesn't apply. \$\endgroup\$2017年08月05日 18:42:26 +00:00Commented Aug 5, 2017 at 18:42
-
\$\begingroup\$ I disagree. I think they can leave it on the stack if implicit output is available. There is no way to check if it's left on the stack otherwise. \$\endgroup\$MD XF– MD XF2017年10月31日 16:57:01 +00:00Commented Oct 31, 2017 at 16:57
-
1\$\begingroup\$ @MDXF Wouldn't that be equivalent to allowing functions in "normal" languages only to use their return value, if the language has implicit output for return values, otherwise there's no way to check if the return value actually has the correct output? \$\endgroup\$2017年10月31日 17:52:00 +00:00Commented Oct 31, 2017 at 17:52
-
\$\begingroup\$ Yeah, like BIT++ \$\endgroup\$kepe– kepe2018年09月15日 18:25:16 +00:00Commented Sep 15, 2018 at 18:25
Assembly programs may write output to some specified memory location
If there are no I/O devices available, an answer might consist of a subroutine that writes its output values to some specified memory location (e.g. write a machine word to 0000ドル
).
-
\$\begingroup\$ I think this includes writing to a buffer for outputting multiple values. \$\endgroup\$qwr– qwr2018年07月12日 02:23:40 +00:00Commented Jul 12, 2018 at 2:23
-
1\$\begingroup\$ I feel like all these similar posts could be combined into something like "Programs may use registers/variables/memory locations for input/output if no input/output device is available". That would cover assembly programs as well as SQL and other higher level languages with the same problems. \$\endgroup\$12Me21– 12Me212018年12月30日 19:00:01 +00:00Commented Dec 30, 2018 at 19:00
Input and output of a single value may be represented as a singleton list
For example, if the value was 4
, [4]
would be an acceptable representation (using Python list syntax).
-
5\$\begingroup\$ This is useful in array languages like J. \$\endgroup\$Esolanging Fruit– Esolanging Fruit2017年04月26日 16:51:34 +00:00Commented Apr 26, 2017 at 16:51
Submissions may use list of strings instead of multi-line strings
Input or output may be the equivalent of the string separated by newlines. For example, the multiline string:
1
2
Fizz
4
Buzz
May be instead be taken as a list of strings like:
["1","2","Fizz","4","Buzz"]
-
2\$\begingroup\$ I always see comments about this in multiline challenges, so I thought I'd throw this up there and see if it is well-recieved as a standard form of input \$\endgroup\$Jo King– Jo King2018年11月13日 11:54:13 +00:00Commented Nov 13, 2018 at 11:54
-
18\$\begingroup\$ I would add an exception for ascii-art as linebreaks can be an important part of those challenges. \$\endgroup\$lirtosiast– lirtosiast2018年11月19日 19:43:32 +00:00Commented Nov 19, 2018 at 19:43
-
\$\begingroup\$ @lirtosiast I don't understand this remark about exceptions for ascii-art. I have written several ascii-art challenges and I don't see why linebreaks need to be treated differently there? (Obviously any challenge can override the defaults specified here anyway...) \$\endgroup\$Steve Bennett– Steve Bennett2025年05月27日 14:48:13 +00:00Commented May 27 at 14:48
In languages without any method of input (e.g. ///) programs may get input through an insertion into the source code
-
3\$\begingroup\$ @DLosc What about code in /// that requires changing at the end? E.g. a simple program that adds a full stop to the end of a string if there isn't already a full stop there
/.;/;//x///;/.x;/(input goes here);
wherex
is a special character (maybe a newline) that is guaranteed not to be in the input. \$\endgroup\$boboquack– boboquack2016年11月20日 09:34:12 +00:00Commented Nov 20, 2016 at 9:34 -
10\$\begingroup\$ This is called hard coding. \$\endgroup\$bb216b3acfd8f72cbc8f899d4d6963– bb216b3acfd8f72cbc8f899d4d69632017年02月28日 12:33:22 +00:00Commented Feb 28, 2017 at 12:33
-
1\$\begingroup\$ @boboquack Can we allow hard-coded input in languages that support input commands? \$\endgroup\$user85052– user850522019年08月25日 11:38:39 +00:00Commented Aug 25, 2019 at 11:38
-
1\$\begingroup\$ That's a good question, and from experience I would guess no, but you should search on meta/ask your own question here. @A_ \$\endgroup\$boboquack– boboquack2019年08月26日 04:05:49 +00:00Commented Aug 26, 2019 at 4:05
-
\$\begingroup\$ Does this include 2d programming languages and/or cellular automata? \$\endgroup\$2020年10月02日 20:35:20 +00:00Commented Oct 2, 2020 at 20:35
-
1\$\begingroup\$ @RedwolfPrograms It's been a long time since I've thought about this, but I would assume yes. \$\endgroup\$boboquack– boboquack2020年10月03日 04:30:06 +00:00Commented Oct 3, 2020 at 4:30
-
\$\begingroup\$ @boboquack I'm going to post a separate set of answers, as it seems different enough the consensus may differ. Thanks for the quick reply! \$\endgroup\$2020年10月03日 14:26:37 +00:00Commented Oct 3, 2020 at 14:26
You must log in to answer this question.
Explore related questions
See similar questions with these tags.