18
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ This comment seems relevant. \$\endgroup\$ – Poke Nov 27 '17 at 20:48
  • \$\begingroup\$ 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. \$\endgroup\$ – Potato44 Nov 27 '17 at 20:53
33
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ Not to mention that similar things are allowed for Java lambdas. \$\endgroup\$ – Nissa Nov 29 '17 at 14:15
1
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Sriotchilism O'Zaic Dec 7 '17 at 17:19
  • \$\begingroup\$ @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 \$\endgroup\$ – Michael Boger Dec 7 '17 at 21:27

You must log in to answer this question.

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