6
\$\begingroup\$

This question already has an answer here:

The vote on this output option has moved here.

Haskell has a very strong type system, so there is a type called Maybe to work around some restrictions.

The Maybe type is defined as follows:

data Maybe a = Just a | Nothing
    deriving (Eq, Ord)

It allows the programmer to specify something may not be there.

From the Haskell wiki.

Essentially, instead of erroring out, a function can return a Maybe that is either a Just x where x is the intended output or it can return Nothing, which indicates that there is no possible output.

One example is the second solution in one of my answers. As you can see, in the output, the answer asked for is Just prefixed.

This can be fixed by applying foldl1 seq to the result, which I think is the golfiest way to do this.

Is outputting a Maybe valid?

Maybe we can allow this...

\$\endgroup\$

marked as duplicate by Nathan Merrill, Mego, caird coinheringaahing, ETHproductions, HyperNeutrino Nov 14 '17 at 22:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    \$\begingroup\$ This can be generalised to any kind of Optional Type, that could either return x or nil or something equivalent. \$\endgroup\$ – Mr. Xcoder Nov 11 '17 at 15:47
  • 1
    \$\begingroup\$ @NathanMerrill There needs to be a consensus before the post goes in there, no? \$\endgroup\$ – totallyhuman Nov 11 '17 at 23:18
  • \$\begingroup\$ No. That post is where we decide the consensus. Occasionally, if the issue is nuanced, or is hard to express using only a single up/down vote, then a secondary post occurs. \$\endgroup\$ – Nathan Merrill Nov 12 '17 at 3:58
  • 3
    \$\begingroup\$ Well, I'd say I'd rather have a complete meta post with 2 (or more answers) than stuffing the same answers into the Default I/O post. Are you suggesting posting multiple options on that post? \$\endgroup\$ – totallyhuman Nov 12 '17 at 14:12
  • 1
    \$\begingroup\$ Sorry, missed your last comment. I'm suggesting that we post multiple answers to that post (one for each option). We currently do that for pretty much every default. \$\endgroup\$ – Nathan Merrill Nov 14 '17 at 17:04
  • \$\begingroup\$ ...Okay... does the clear consensus on this post still count now that it's a duplicate? \$\endgroup\$ – totallyhuman Nov 15 '17 at 21:17
  • \$\begingroup\$ Nitpick: Defining Maybe as mentioned in the Haskell wiki is not the way it is defined. Try data Mby a = Jst a | Nope deriving (Eq, Ord) and then (+1) <$> Jst 41 which will complain that Mby is not an instance of Functor whereas the real Maybe certainly is and it implements a lot of other classes too (check :info Maybe in ghci).. \$\endgroup\$ – ბიმო Nov 22 '17 at 16:34
15
\$\begingroup\$

Yes

Maybe is used to emulate failure in Haskell. The equivalent question in, say, Python, is

is it OK that a submission could, in theory, return None, even though it never does?

or

is it OK that a submission could, in theory, raise an exception, even though it never does?

The answers to these are obvious.

What the type system allows the submission to do ≠ what the submission actually does.

Also, a Maybe is equivalent to a list restricted to 0 or 1 elements, so the consensus about singleton lists could apply here.

\$\endgroup\$
  • \$\begingroup\$ Another valid "wrapper" is curried return types. We allow returning a function that returns the value. \$\endgroup\$ – Nathan Merrill Nov 14 '17 at 18:09
  • \$\begingroup\$ So this counts for the [] too then because it is often used to emulate failure as well? For example returning a wrapped in a list for a success or the empty list in case of a failure. \$\endgroup\$ – ბიმო Nov 22 '17 at 16:36
  • \$\begingroup\$ Can I ask you to post this answer on the Default I/O post? \$\endgroup\$ – totallyhuman Nov 23 '17 at 14:06
  • 1
    \$\begingroup\$ @totallyhuman Added. \$\endgroup\$ – Esolanging Fruit Nov 27 '17 at 1:03
1
\$\begingroup\$

As long as the function is guaranteed to not output Nothing, it is valid

The title explains it all. If the function is not supposed to return Maybe and it is guaranteed to not output Nothing, it is valid.

\$\endgroup\$

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