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