Question in brief: For what challenge types and in what circumstances should code that requires a REPL be acceptable in an answer?

There seems to be an understanding in chat that code that only works in a REPL is not valid as an answer, but I can't find any consensus here on meta to back this up, which makes it difficult to comment to that effect on REPL answers.

I've found these two somewhat contradictory posts:

This question:

Default for Code Golf: Program, Function or Snippet?

and this answer elsewhere:

Loopholes that are forbidden by default: Posting a code snippet instead of a complete answer

However, mixing up the discussion and voting with the program/function debate seems to be causing confusion, so I thought this could use its own question.

  • 4
    \$\begingroup\$ Pro: A REPL could basically be thought of as a separate programming language anyway, and it's silly to expect people to create "PythonREPL" and "RubyREPL" etc. for every language that simply runs the code and prints the result. Con: Code that requires a REPL cannot be run from the command line easily and is more of a snippet than an actual program/function. \$\endgroup\$ – Doorknob Dec 28 '15 at 23:55
  • 1
    \$\begingroup\$ Not sure if this is a coincidence, but I wanted to post this question literally 5 minutes ago due to this discussion \$\endgroup\$ – March Ho Dec 29 '15 at 0:01
  • 2
    \$\begingroup\$ An important thing to consider is languages like J which I think only exists in a REPL environment. \$\endgroup\$ – Martin Ender Dec 29 '15 at 0:01
  • 1
    \$\begingroup\$ @MartinBüttner J can exist as POSIX scripts, but Dyalog APL, for example, does not have full programs in the usual sense. \$\endgroup\$ – Dennis Dec 29 '15 at 1:19
  • 1
    \$\begingroup\$ @MarchHo that answer wasn't the one that prompted this meta discussion, but the one that did was another answer to the same question... \$\endgroup\$ – trichoplax Dec 29 '15 at 14:00

Always— but a REPL is a different language.

We usually define languages by their interpreters. My Python shell is an interpreter, which satisfies our definition of a programming language. Therefore, it is a valid language.

"Wait a minute", you say, "won't REPLs usually be shorter than their full program/function equivalents?" Why won't people golf in 'Python REPL' over Python?

That will happen, and it's fine. Clever and well-golfed full-program Python answers will still be upvoted—just as they are now despite golfing languages usually winning challenges.

In addition, a REPL entry won't always be a parent language entry without the print(). The REPL often has interesting features that can be used cleverly, and golfing in a REPL means a different set of constraints than in the parent language. For example, _ in a Python REPL is the last evaluated expression, which is shorter than assigning to a variable.

  • 1
    \$\begingroup\$ With Python it is possible to type a full multiline program into the REPL, and still take advantage of not needing to type "print" to output results. \$\endgroup\$ – trichoplax Dec 29 '15 at 14:03
  • 1
    \$\begingroup\$ @trichoplax Is there a problem with that? \$\endgroup\$ – lirtosiast Dec 29 '15 at 15:34
  • 1
    \$\begingroup\$ If this is accepted, there are two possible outcomes: 1. Many contests will have an answer in Python and another on in Python Shell. This is not very interesting. 2. Most Python golfers will convert to Python Shell golfers. If we wanted that, we shouldn't have outlawed snippets in the first place. \$\endgroup\$ – Dennis Dec 29 '15 at 15:43
  • 6
    \$\begingroup\$ @Dennis I see your point (I would discourage golfing in REPL environments), but a shell is an interpreter and therefore a language. Excluding shells, just because they're shells and often shorter, is tantamount to adding them to the list of forbidden languages, for which there is insufficient justification. \$\endgroup\$ – lirtosiast Dec 29 '15 at 15:51
  • \$\begingroup\$ @ThomasKwa it wasn't intended as a good or bad point, just background in case it affects people's view. \$\endgroup\$ – trichoplax Dec 29 '15 at 15:56
  • 4
    \$\begingroup\$ @ThomasKwa I think we're not excluding shells because they're shells, but because "full programs" can be stored in files whose encoding and size can be easily determined which is not necessarily true for code typed into a shell. \$\endgroup\$ – Martin Ender Dec 30 '15 at 20:58
  • 1
    \$\begingroup\$ Reasons for upvote: 1. Banning languages is always a bad idea. 2. I can think of 2 languages that only exist as loops: Processing (based on Java) and Arduino C (based on C.) I think both \$\endgroup\$ – Level River St Jan 4 '16 at 20:21
  • 5
    \$\begingroup\$ I'm unsure about this. I agree that philosophically banning REPLs is unjustified, as they are languages like all others. But I think that votes are liable to cause one of two bad cases: REPL answers get unjustly downvoted simply because some voters don't like the idea of REPL, or they get upvoted more than an equivalent non-REPL answer would, pushing posters to make often-trivial modifications to REPL-ify their code. \$\endgroup\$ – xnor Jan 10 '16 at 21:30
  • 2
    \$\begingroup\$ Do Enter keypresses need to be counted as 1, or not be counted at all? (Enter, not Shift+Enter, which definitely counts as 1) Because Enter is the key that sends statements and expressions to the REPL. I think it would be "Yes count Enter keypresses. Subtract 1 from the total score." \$\endgroup\$ – Erik the Outgolfer Sep 15 '16 at 18:31
  • 2
    \$\begingroup\$ What if I created a REPL language that counted the number of newlines and then interpreted it as a program in Unary? You now have a golfing language where every program is length 0 (You might need a termination character, in which case length 1). \$\endgroup\$ – Esolanging Fruit Nov 1 '16 at 0:05
  • \$\begingroup\$ @lirtosiast Is there a list of forbidden languages? \$\endgroup\$ – Jonathan Frech Aug 29 '18 at 8:01

I believe that if the programming language has a significant proportion of its code being run in a REPL environment, using the REPL form of the code should be allowed, unless it is specifically disallowed in the question.

Examples of such "significantly REPL languages" include Mathematica, where the code is run within "notebooks", which are REPL environments.

Examples of languages where REPL is not active in the majority of usage cases include C++ and PHP (although as far as I am aware both of them can also be run in a REPL shell).

  • \$\begingroup\$ What about JavaScript/Python? \$\endgroup\$ – Martin Ender Dec 29 '15 at 0:10
  • \$\begingroup\$ @MartinBüttner If we can get consensus on this answer, I am sure there will be opportunities for the community to decide whether certain less well-defined languages are "significantly REPL" or not. \$\endgroup\$ – March Ho Dec 29 '15 at 0:13
  • 2
    \$\begingroup\$ I'm uncomfortable with basing this decision on whether a REPL is commonly used. Is this your intended meaning? \$\endgroup\$ – trichoplax Dec 29 '15 at 14:17
  • \$\begingroup\$ @trichoplax yes. I suppose it is much more meaningful than any currently available alternative. Feel free to suggest other possibilities. \$\endgroup\$ – March Ho Dec 29 '15 at 15:04
  • \$\begingroup\$ IMO most mathematica is not in a notebook unless you need to do some really quick calculations \$\endgroup\$ – user46167 Dec 29 '15 at 16:03
  • 2
    \$\begingroup\$ @BlockCoder1392 I have personally not seen anyone using Mathematica outside the notebook environment, and furthermore Mathematica's default file extension is .nb. Can you point out any widely used examples of programming in Mathematica outside of the notebook/kernel REPL environment? \$\endgroup\$ – March Ho Dec 30 '15 at 12:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .