<div dir="ltr">The IO Monad provides a way to construct imperative programs in a pure manner. For example,<div><br></div><div><font face="monospace, monospace"> print x = putStrLn (show x)</font></div><div><br></div>The function 'print' can be thought of as returning the same 'IO computation' for same values of x.<div>Using the Monad interface, these instructions can be combined, like</div><div><br></div><div><font face="monospace, monospace"> main = getLine >>= print</font></div><div><font face="monospace, monospace"><br></font></div>which is equivalent to<div><br><div><font face="monospace, monospace"> main = do</font></div><div><font face="monospace, monospace"> x <- getLine</font></div><div><font face="monospace, monospace"> print x</font></div><div><br><div><div>Just like this, many IO values can be stitched together to create larger IO values, which are descriptions of imperative programs as above.</div></div></div></div><div>It is pure, as the same 'computation' is returned for the same arguments.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 26 April 2015 at 15:46, Jakob Holderbaum <span dir="ltr"><<a href="mailto:mailings@jakob.io" target="_blank">mailings@jakob.io</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Shishir,<br>
<br>
I explain it to myself as the nondeterministic behavior of the user which would lead to the impurity.<br>
<br>
>From the perspective of the programm, you just call a function without argument that returns a string.<br>
And every call has the potential to return something totally different depending on the users behavior.<br>
<br>
So what you called input is not the input in the sense of an functional argument which is the foundation of the definition of purity.<br>
<br>
I am myself an absolute beginner in Haskell and Functional Thinking so I hope to get some constructive feedback on my mental image of this situation.<br>
<br>
Cheers<br>
Jakob<br>
<div><div class="h5"><br>
On 04/26/2015 11:59 AM, Shishir Srivastava wrote:<br>
> Hi,<br>
><br>
> Can someone please explain how IO operations do not fit in the pure<br>
> category of mathematical function in that they have to be implemented via<br>
> Monads.<br>
><br>
> For e.g. the getLine function has the type IOString and it reads the input<br>
> from the user. Now as I see it the output of getLine will always be same if<br>
> the input remain same (i.e. for input "X" getLine will always return "X" )<br>
> which is the constraint on mathematical functions.<br>
><br>
> Therefore I don't see why monads are necessary for implementing IO in pure<br>
> languages.<br>
><br>
> I can understand why Date and Random functions have be implemented via<br>
> monads because their output will always change.<br>
><br>
> Thanks,<br>
> Shishir<br>
><br>
><br>
><br>
</div></div>> _______________________________________________<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>
<br>
--<br>
Jakob Holderbaum, M.Sc.<br>
Embedded Software Engineer<br>
<br>
0176 637 297 71<br>
<a href="http://jakob.io/" target="_blank">http://jakob.io/</a><br>
<a href="http://jakob.io/mentoring/" target="_blank">http://jakob.io/mentoring/</a><br>
<a href="mailto:hi@jakob.io">hi@jakob.io</a><br>
@hldrbm<br>
<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>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Regards</div><div dir="ltr"><div><br></div><div>Sumit Sahrawat</div></div></div></div></div></div></div>
</div>