# How do we treat default output formats such as “ans = …” etc.?

In MATLAB and Octave, the default output looks like this (I guess this is relevant for other languages as well):

'This is a string'

ans =

This is a string

x = 'This is a string'

x =

This is a string


Using disp will give:

disp('This is a string')
This is a string


There are, as far as I can tell, (at least) three types of challenges where this is relevant:

1. The output should look exactly like this (maybe newlines and trailing spaces are OK)
• This one is bankers: ans = ... is not OK
2. Remove elements, remove letters, etc. from an input string.
• I believe ans = ... should be OK, since the output format is not specified.
3. "Output: A is greater than B if A > B", copy from STDIN to STDOUT
• ?

What do we do when the question simply states:

• "Output a string"
• "Print to STDOUT"

This is quite important, since disp('String') is much longer than just String. Also, for those cases where ans = ... is OK, will x = ... also be OK?

# Update:

There are some cases that have come up recently:

1. The following function returns no arguments, but will write x = 1 2 3 4 5 in the command window:

function f;x=1:5


I'm assuming this is disallowed?

2. The following is not a function, but also doesn't use disp. It results in the integers 1-10 being printed like i = 1, i = 2 etc. clearing the screen between each new number.

for i=1:10,pause(1);clc;i,end


I'm assuming this is disallowed?

• This can also apply to ex. Ruby. p"Hello, World!" saves three chars over puts"Hello, World!" but outputs the string with quotes around it (and would also escape any backslashes, quotes, etc. in the string if there were any). – Doorknob Nov 2 '15 at 21:28
• Note that is a REPL output, which is invalid anyway. :P – Rɪᴋᴇʀ Apr 28 '16 at 19:11
• Also Mathematica's Echo which could be used instead of Print if not for the >> – Pavel Dec 10 '16 at 2:53
• On this answer, I raise something similar in my new language. – caird coinheringaahing Mar 28 '17 at 21:10

"Output a string" typically implies that it can be returned from a function, in which case extraneous output like ans = and x = is irrelevant. But in any situation in which a full program is used, be it required or by choice, or a function that prints to STDOUT rather than returning the value is used, disp is required.

• Not sure if I understand where the limit goes here... -in your opinion, is it OK to have ans = cat goes "Meow" in the cat-challenge? Is it OK to have x = cat ...? – Stewie Griffin Nov 2 '15 at 16:35
• A function never prints to STDOUT without the prefix ans = ..., unless the function is disp() of course. – Stewie Griffin Nov 2 '15 at 16:44
• @StewieGriffin Right. I mean that ans = and x = shouldn't be there if the function is supposed to print something rather than return something. – Alex A. Nov 2 '15 at 16:45
• Ok, just a little final clarification: In the A is greater than B challenge, ans = ...  is OK. – Stewie Griffin Nov 2 '15 at 16:50
• @StewieGriffin It appears in that case you opted for a full program rather than a function, so I would say that ans shouldn't be there. – Alex A. Nov 3 '15 at 0:25
• I've added an update to the question. What's right here (in your opinion)? – Stewie Griffin Dec 10 '16 at 0:29
• @StewieGriffin If I'm not mistaken, both of the examples you cite in the update rely on Matlab being run in the REPL, right? That is, neither would print if the program were run as a script instead? – Alex A. Dec 10 '16 at 19:53
• MATLAB is both a language and a program. You don't (normally) run a MATLAB script/function from outside the GUI (or CLI if Octave). Functions and "regular scripts" behaves the same way in MATLAB, the (main) difference is that variables inside a function are private. A line such as: x=1 that's not terminated by a semicolon will print x = 1 to the command window either way. The difference is, if it's a function and you don't explicitly return it then you can't use x to anything afterwards. – Stewie Griffin Dec 10 '16 at 20:50
• @StewieGriffin Thanks for the explanation. I'd be inclined to say that the examples you give in the post should be okay then. – Alex A. Dec 13 '16 at 3:46

This depends on how strict the output requirements of the challenge are.

• Some challenges have very relaxed requirements on the output format. E.g. some just specify "any convenient, unambiguous format". In those cases, including ans = or x = in the output is fair game as it does in no way obscure the actual output and is quite a natural output for Matlab as well.
• Other challenges require a very exact output format, e.g. a specific string (think "Hello, World!"). Those will usually state that the output must contain nothing except what is specified. In this case ans = and x = are not fine unless they happen to match exactly the required output format.
• Some challenges will require such an exact output format, but make an exception for any fixed interpreter output which cannot possibly be suppressed. This exception would not include Matlab, as ans = and x = can be suppressed using ; and printing with disp or similar instead.
• Apologies if this is a stupid question, but what to do in challenges such as Write a "program that copies all STDIN to STDOUT, but removes capital letters"? – Stewie Griffin Nov 2 '15 at 17:05
• @StewieGriffin Such a challenge will probably specify that there should be no extraneous output, so you'd have to use disp. If the challenge does not specify that, you might want to ask for clarification. – Martin Ender Nov 2 '15 at 17:15
• I don't like reading into the exact wording used by the poster to determine what's allowed. Chances are the choice of phrase for the output is incidental, and in any case, made without thought as to this issue. In your examples, the phrasing is very specific, but I imagine few posts would go into that level of detail of how they want the output, nor would I want to have to do so in writing a challenge. I'd prefer any consistent default ruling over this. – xnor Nov 2 '15 at 21:03