[Haskell-beginners] Maybe and Just
Mike Meyer
mwm at mired.org
Fri Mar 27 10:00:08 UTC 2015
On Fri, Mar 27, 2015 at 4:44 AM, Shishir Srivastava <
shishir.srivastava at gmail.com> wrote:
> Sorry, but I still have some grudges so to say with the way 'Maybe' is
> defined.
>
> By definition '*Maybe*' takes a type parameter which I will denote here
> as '*tp*' for the sake of clarity like below -
>
> data Maybe tp = Just tp | Nothing
>
> Therefore '*tp*' is not representing a value perse such as '3' or 'XYZ'
> etc but a data type such as '*Int*', '*Char*' etc.
>
> But when we have to create a data type of '*Maybe*' we write '*Just 3*'
> and not '*Just Int*' or '*Just Char*' which is how it's defined in the
> definition (i.e. '*Just tp*' ) .
>
> It is this discrepancy in definition and the actual value creation which
> is slightly troubling me.
>
This was already explained, all be it in passing.
Maybe Int is a type. Just and and Nothing are functions. Nothing takes no
arguments. Just takes a single argument of the type given. There IS no type
that Just Int would represent. There are only the two types Int and Maybe
Int. 3 is of type Int. Just 3 is of type Maybe Int. Nothing is of type
Maybe Int as well.
Going back to the notion of "type plus error value", Int is the type, and
Maybe Int is the type with an error value, the error value being Nothing.
It might be educational to look at a second example. Try the Either types,
which are sometimes used in a manner similar to the Maybe types. But
instead of Nothing to indicate "an error occurred", you have Left tp to
indicate that an error occurred using the value of tp to provide
information about the error.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150327/85d1b934/attachment.html>
More information about the Beginners
mailing list