[Haskell-beginners] Maybe and Just

Mike Meyer mwm at mired.org
Thu Mar 26 10:28:40 UTC 2015


On Thu, Mar 26, 2015 at 5:06 AM, Shishir Srivastava <
shishir.srivastava at gmail.com> wrote:

> Hi,
>
> After reading and re-reading the haskell tutorials I don't happen to see a
> very convincing or appealing reason for having these data types.
>
> Can anyone please explain where Maybe and Just provide the sort of
> functionality that cannot be achieved in other languages which don't have
> these kind.
>

When a function fails to produce the return value, it can either return a
distinguished value (a NULL pointer, or -1 to signal an error, or ...) or
raise an exception. The latter is sufficiently clumsy that it's not
uncommon to see languages with two versions of some functions: one that
raises an exception, and one that returns a distinguished value.

Haskell uses the Maybe type for the distinguished value, raising it to the
type level. If I call a function that expects a value that is NOT the
distinguished value (a valid pointer, or a valid array index, or ...) in
languages without something like a Maybe type, I find out about it when my
unit tests fail if I'm lucky, or I just  get the wrong answer if I'm not.
When I call a function that expects an X with a value of type Maybe X in
Haskell, it's a type error at compile time.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150326/2c882605/attachment-0001.html>


More information about the Beginners mailing list