For many code golf questions, we allow functions as answers. What exactly does it mean for some code to be a valid submission as a function?
This question came up during a discussion on built-ins, but is a separate discussion from that one.
Here are some examples in Python, where I'm not sure which should be allowed.
Cases that seem clearly allowed:
Defining a wholly new function, where defining means giving the function a name by which it can be later called:
limit = 10 def func(a): for i in range(limit): if a[i % len(a)]: return i
An expression which evaluates to a function:
lambda a:a.count(a[0])/len(a)
Cases that seem like they shouldn't be allowed:
Causing a function to exist and be usable, but without interacting with it directly:
import math
This causes the function
math.gcd
to exist, without directly interacting with it.This case is particularly problematic, because a golfing language could be defined to cause literally every possible function to become defined when given a 1 (or 0) character command, which defeats the purpose.
Causing a very large number of functions to be defined, where the function in question is one of them:
for index, code in enumerate(all_possible_strings): eval("func" + str(index) + "=" + code)
Assume that all_possible_strings is a generator which yields all possible strings. Somewhere in that sequence, the string
lambda a:a.count(a[0])/len(a)
must have occurred, so the functionfunc<very large number>
is now defined as that function. This also seems problematic and like a loophole.
Cases I'm not sure about/what the consensus is:
Submissions that contain an expression that evaluates to the desired function, as well as other code:
A = [1] for i in range(50): A.append(sum(A) * i) A.count
Supposing
A.count
solved the challenge, would this be valid?
Given these cases, what should our standard of a valid function submission be?
eval("func"+str(i)+"="+index)
? \$\endgroup\$