# Is outputting a Maybe acceptable in Haskell? [duplicate]

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.

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...

• This can be generalised to any kind of Optional Type, that could either return x or nil or something equivalent. Nov 11, 2017 at 15:47
• @NathanMerrill There needs to be a consensus before the post goes in there, no? Nov 11, 2017 at 23:18
• 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. Nov 12, 2017 at 3:58
• 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? Nov 12, 2017 at 14:12
• 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. Nov 14, 2017 at 17:04
• ...Okay... does the clear consensus on this post still count now that it's a duplicate? Nov 15, 2017 at 21:17
• 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).. Nov 22, 2017 at 16:34

# 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.

• Another valid "wrapper" is curried return types. We allow returning a function that returns the value. Nov 14, 2017 at 18:09
• 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. Nov 22, 2017 at 16:36
• Can I ask you to post this answer on the Default I/O post? Nov 23, 2017 at 14:06
• @totallyhuman Added. 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.