<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif;font-size:small">Hi, Shishir,</div><div class="gmail_default" style="font-family:georgia,serif;font-size:small"><br></div><div class="gmail_default" style="font-family:georgia,serif;font-size:small">As a recovering complexity addict (i.e. Java programmer ;-), I'd put it this way.</div><div class="gmail_default" style="font-family:georgia,serif;font-size:small"><br></div><div class="gmail_default" style="font-size:small"><span style="font-family:georgia,serif">There are many methods (on many types) that can either succeed and return a meaningful value, or be unable to fulfill the intent of the caller's request. Example include </span><font face="monospace, monospace">indexOf</font><font face="georgia, serif"> on </font><font face="monospace, monospace">String</font><font face="georgia, serif">, </font><font face="georgia, serif"> </font><font face="monospace, monospace">get</font><font face="georgia, serif"> on </font><font face="monospace, monospace">Map</font><font face="georgia, serif">, and </font><font face="monospace, monospace">read</font><font face="georgia, serif"> on a </font><font face="monospace, monospace">Reader</font><font face="georgia, serif">. Every time such a partial function scenario occurs, the author must decide what to do if the intent can't be fulfilled. That freedom of choice has led to a proliferation of techniques.</font></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif"><br></font></div><div class="gmail_default" style="font-size:small"><ul><li><font face="monospace, monospace">indexOf</font><font face="georgia, serif"> returns </font><font face="monospace, monospace">-1</font><font face="georgia, serif">, which is a valid </font><font face="monospace, monospace">int</font><font face="georgia, serif">, but not a valid position within a string.</font><br></li><li><font face="monospace, monospace">get</font><font face="georgia, serif"> returns </font><font face="monospace, monospace">null</font><font face="georgia, serif">, which is the only value common to all reference types that means 'I have nothing'. Of course, that also prevents one from storing </font><font face="monospace, monospace">null</font><font face="georgia, serif"> in a </font><font face="monospace, monospace">Map</font><font face="georgia, serif"> and simply retrieving it. Worse, if that result is passed along elsewhere, one can eventually receive a </font><font face="monospace, monospace">NullPointerException</font><font face="georgia, serif"> occurring in a completely different part of the code.</font><br></li><li><font face="georgia, serif">And, <b><i>horrors!</i></b>, </font><font face="monospace, monospace">read</font><font face="georgia, serif"> responds in a totally different way, by forcing the caller to "jump" somewhere.</font><br></li></ul></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif"><br></font></div><div class="gmail_default" style="font-size:small"><font face="monospace, monospace">Maybe</font><font face="georgia, serif"> provides a general, reusable solution to all such cases.</font></div><div class="gmail_default" style="font-size:small"><ul><li><span style="font-family:georgia,serif">It allows one to "stretch" any type to include an extra "out of band" value.</span><br></li><li><span style="font-family:georgia,serif">The type declaration clearly (uniformly) notifies anyone else that the function may have to return "lack of result".</span><br></li><li><span style="font-family:georgia,serif">The type system prevents a caller from simply assuming that a result is present, and then accidentally propagating failure.</span><br></li><li><font face="georgia, serif">Because </font><font face="monospace, monospace">Maybe</font><font face="georgia, serif"> is an instance of </font><font face="monospace, monospace">Functor</font><font face="georgia, serif">, one can use </font><font face="monospace, monospace">map</font><font face="georgia, serif"> to safely apply a function (or do nothing gracefully) without littering the calling code with </font><font face="monospace, monospace">if</font><font face="georgia, serif">-statements.</font><br></li><li><span style="font-family:georgia,serif">etc.</span><br></li></ul></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif"><br></font></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif">I don't think of </font><font face="monospace, monospace">Maybe</font><font face="georgia, serif"> as enabling a <i>computation</i> that is impossible otherwise, but I do regard it as providing a <i>consistency and simplicity</i> that is otherwise not present.</font></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif"><br></font></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif">Hope this helps,</font></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif">Joel</font></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif"><br></font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 26, 2015 at 5:06 AM, Shishir Srivastava <span dir="ltr"><<a href="mailto:shishir.srivastava@gmail.com" target="_blank">shishir.srivastava@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi, </div><div><br></div><div>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. </div><div><br></div><div>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.</div><div><br></div><div>Thanks,</div><div><div><div dir="ltr"><font color="#0b5394"><font style="background-color:rgb(255,255,255)"><font face="georgia, serif">Shishir Srivastava</font></font><br></font><br></div></div></div>
</div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Beauty of style and harmony and grace and good rhythm depend on simplicity. - Plato</div>
</div>