9
\$\begingroup\$

We allow both programs and functions. Some challenges, mostly challenges, do not require input. This means that for some challenges you can write a function that takes no input. In many languages this is rather straight forward. For example if a challenge asked you to output 26 python function to do these it would look like this:

lambda:26

In Haskell this is a bit stranger. There are two arguments to be had:

  • Haskell does not have functions that take no inputs so you must write a complete program. You should write:

    main=print 26
    
  • Everything in Haskell is a function, input-less functions are just objects. You can just write:

    26
    

    and that's a function that takes no args and returns 26.

I don't very much like either of these arguments. The first seems to me to be treating Haskell as if it were Python while the second seems to be arguing that snippets are ok, which I certainly don't believe.

What should be allowed in Haskell for challenges that don't allow input?

Before anyone says, "Why does this even matter? A challenge this simple would be closed anyway." Often these types of challenges have further stipulations for example this question requires each entry only to output a number, but has source restrictions on each answer.

\$\endgroup\$
4
  • 4
    \$\begingroup\$ +1 I planned to ask this question on meta myself once I found the time to formulate the problem. My current remedy (which most likely prompted your question as I used it in your answer-chaining challenge) is to write a function f _=26 which can take any argument and then returns 26. You can call it with f(), which looks kind of natural for functions one might be used to from other language, but it really isn't the Haskell way ... \$\endgroup\$
    – Laikoni
    Commented Jun 16, 2017 at 12:19
  • 1
    \$\begingroup\$ Related: Are we allowed to use empty input we won't use when no input is asked (regarding functions)?. The consensus there allows something like f _=26 as I wrote in the comment above. \$\endgroup\$
    – Laikoni
    Commented Jun 20, 2017 at 17:53
  • \$\begingroup\$ This is relevant in Clean (and I imagine other similar languages) as well. \$\endgroup\$
    – Οurous
    Commented Feb 5, 2018 at 5:47
  • \$\begingroup\$ Gah somehow I missed this question and have still been adding _s all over the place. \$\endgroup\$ Commented May 14, 2018 at 17:15

2 Answers 2

20
\$\begingroup\$

As stated in the comments I have been undecided about this matter, but thinking a bit lead to the following argument in favour of the second option:

In Haskell 26 is a valid anonymous function which takes no argument

Haskell's laziness makes each object behave like a function which only returns its value when called, even though the call needs no arguments and happens implicit.

Consider the declaration x=mod 1 0. Division by 0 causes an exception, but only when the value is actually computed. Because of the lazy evaluation, the declaration causes no exception, just like a function declaration x=lambda:1/0 in Python does not cause an error.

Only if we force x to yield its value, e.g. by writing print x, the exception is thrown, just like print(x()) in Python throws an error. Thus x can be considered to be a function.

However, as the site rules allow anonymous functions, in the above case mod 1 0 then is an anonymous function, as is 26 or [1,2,3].


I do see the problem that this is essentially allowing snippets in Haskell, but this is different than allowing snippets in general. Rather the proposal would be to recognize lazy evaluation as implicit function evaluation.

This would solve the problem for Haskell and potential other languages that support lazy evaluation, though it would still persist e.g. in Standard ML which also has no dedicated syntax for functions taking no arguments, but also no strong laziness concept.

\$\endgroup\$
-5
\$\begingroup\$

If 26 evaluates to a function which takes no arguments and returns the number 26, then it is a valid function submission.

\$\endgroup\$
1
  • 5
    \$\begingroup\$ Whether or not it does is really a matter of opinion in Haskell. Functions are not called with parentheses in Haskell so both arguments make sense. \$\endgroup\$
    – Wheat Wizard Mod
    Commented Jun 16, 2017 at 12:22

You must log in to answer this question.

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