# Are functions that only work when called valid as answers?

In Haskell it is possible to define functions that will cause a compiler error unless they are called elsewhere in the program.

For example the following code causes a compiler error

r=sum.map(read.(:[])).show
main=putStr""


Try it online!

while the extremely similar

r=sum.map(read.(:[])).show
main=print\$r 6


Try it online!

has no error.

The reason this occurs is the Haskell compiler fails to find the type signature of the first bit of code because of multiple instances of show, however once you call it on a Num elsewhere the compiler can figure out which instance of show you mean.

The question is, should this type of function, that is functions that need to be called in order to work be considered valid entries to challenges that require a function?

• This comment seems relevant. – Poke Nov 27 '17 at 20:48
• I don't know any off the top of my head but I have seen answers that rely on being able to do this to work. – Potato44 Nov 27 '17 at 20:53

# Yes

Your code only has to handle the case where it is called properly. You don't need handle cases where the input is something outside the bounds of the challenge, and the case where your code isn't used at all is no exception.

A challenge generally asks for a given behavior when the function is called, and doesn't care about any other possibility.

• Not to mention that similar things are allowed for Java lambdas. – Stephen Leppik Nov 29 '17 at 14:15

# Yes

I've been told repeatedly that anonymous lambdas are valid answers in python, and they /can't/ be called outside of the statement containing them, and assignment to a variable (which makes them non-anonymous) or embeddedness in a print statement generally isn't counted towards the bytecount. I think your case, where the function can be called or not, but must be called in order to compile, is very similar.

It looks like your function is equivalent to a template function in C++, which technically doesn't even exist unless it's invoked (the compiler produces code depending on the template arguments with which it's actually called), and I'd certainly allow something like that and not bar it on a technicality.

• Your first statement about python lambdas is not exactly correct. Python lambdas can be called directly without any additional context, example. This is part of what makes them anonymous. – Sriotchilism O'Zaic Dec 7 '17 at 17:19
• @WheatWizard I consider the print statement as additional context. What I meant is they're kinda one-shot, I guess, and I will edit my post to reflect this – Michael Boger Dec 7 '17 at 21:27