
There is a subtle difference between Haskell Either and Gleam Result.
Haskell:
data Either a b = Left a | Right b ...
Gleam:
pub type Result(a, e) {
Ok(a)
Error(e)
}
The computer doesn't care, but it's important for human thinking:
** Nothing is *not* an Error.
Suppose for example I have a function
next_smaller_prime :: Int -> Maybe Int
where next_smaller_prime 10 -> Just 7
and next_smaller_prime 2 -> Nothing
The second case is not an error. You get the answer Nothing
because the function *worked*, not because it didn't.
To return Error Nil is to commit the YouTube (social) offence:
"You are an evil-doer who has done something wrong.
I refuse to tell you WHAT you did wrong,
so you can't fix it, you wrong-thinking PEASANT."
Seriously, if you have decided to return Error(x),
x had BETTER be a 'reason' (as Erlang calls it) for WHY it is
an error. The pattern in Erlang is, after all,
{ok,Result} | {error,Reason}.
sans_reason (Left _) = Nothing
sans_reason (Right x) = Just x
with_reason (Nothing) s = Left s
with_reason (Just x) _ = Right x
are trivial conversion functions. As I said, the computer does not care.
There are (at least) three different situations we can consider.
(1) Sometimes there is no answer. Typically a search.
In this case, Maybe is appropriate.
(2) Sometimes you asked a question which fails to have an answer
for a reason.
In this case, Either is appropriate.
(3) Sometimes you asked a sensible question for which the system
might have been expected to produce an answer, but something
went wrong. Numeric overflow, database connection shut down
unexpectedly, hard drive developed a bad block.
In this case, an exception is appropriate.
And of course there are other reasons to use Either. Think of
divide-and-conquer: classify :: Problem -> Either SubProblems EasyProblem.
Because Gleam's Result isn't Haskell's Either in terms of connotations for
human beings, even if they are basically the same to a computer.
On Fri, 29 May 2020 at 22:26, Wiebe-Marten Wijnja
Greetings, everyone!
Recently I was involved in a discussion on the new ML-style language 'gleam'.
Gleam has for quite a while now only had an `Either a b` type, with all functions that in Haskell one would use a `Maybe a` for, working on an `Either a ()` instead.
In the discussion(https://github.com/gleam-lang/gleam/issues/591), the language designers were asking the community whether it would make sense to add `Maybe` to the language as well, or keep using only `Either a ()`.
My question: Is the difference between `Maybe a` and `Either a ()` only semantic and are they functionally equivalent, or are there differences in functionality as well?
Have a nice day,
~Marten / Qqwy
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.