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...
2 Answers 2
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.
-
\$\begingroup\$ Another valid "wrapper" is curried return types. We allow returning a function that returns the value. \$\endgroup\$Nathan Merrill– Nathan Merrill2017年11月14日 18:09:22 +00:00Commented Nov 14, 2017 at 18:09
-
\$\begingroup\$ So this counts for the
[]
too then because it is often used to emulate failure as well? For example returninga
wrapped in a list for a success or the empty list in case of a failure. \$\endgroup\$ბიმო– ბიმო2017年11月22日 16:36:45 +00:00Commented Nov 22, 2017 at 16:36 -
\$\begingroup\$ Can I ask you to post this answer on the Default I/O post? \$\endgroup\$totallyhuman– totallyhuman2017年11月23日 14:06:28 +00:00Commented Nov 23, 2017 at 14:06
-
1\$\begingroup\$ @totallyhuman Added. \$\endgroup\$Esolanging Fruit– Esolanging Fruit2017年11月27日 01:03:00 +00:00Commented Nov 27, 2017 at 1:03
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.
x
ornil
or something equivalent. \$\endgroup\$Maybe
as mentioned in the Haskell wiki is not the way it is defined. Trydata Mby a = Jst a | Nope deriving (Eq, Ord)
and then(+1) <$> Jst 41
which will complain thatMby
is not an instance ofFunctor
whereas the realMaybe
certainly is and it implements a lot of other classes too (check:info Maybe
inghci
).. \$\endgroup\$