[Haskell-beginners] IO and purity

Sumit Sahrawat, Maths & Computing, IIT (BHU) sumit.sahrawat.apm13 at iitbhu.ac.in
Sun Apr 26 10:50:30 UTC 2015


The IO Monad provides a way to construct imperative programs in a pure
manner. For example,

    print x = putStrLn (show x)

The function 'print' can be thought of as returning the same 'IO
computation' for same values of x.
Using the Monad interface, these instructions can be combined, like

    main = getLine >>= print

which is equivalent to

    main = do
      x <- getLine
      print x

Just like this, many IO values can be stitched together to create larger IO
values, which are descriptions of imperative programs as above.
It is pure, as the same 'computation' is returned for the same arguments.

On 26 April 2015 at 15:46, Jakob Holderbaum <mailings at jakob.io> wrote:

> Hi Shishir,
>
> I explain it to myself as the nondeterministic behavior of the user which
> would lead to the impurity.
>
> From the perspective of the programm, you just call a function without
> argument that returns a string.
> And every call has the potential to return something totally different
> depending on the users behavior.
>
> 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.
>
> 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.
>
> Cheers
> Jakob
>
> On 04/26/2015 11:59 AM, Shishir Srivastava wrote:
> > Hi,
> >
> > Can someone please explain how IO operations do not fit in the pure
> > category of mathematical function in that they have to be implemented via
> > Monads.
> >
> > For e.g. the getLine function has the type IOString and it reads the
> input
> > from the user. Now as I see it the output of getLine will always be same
> if
> > the input remain same (i.e. for input "X" getLine will always return "X"
> )
> > which is the constraint on mathematical functions.
> >
> > Therefore I don't see why monads are necessary for implementing IO in
> pure
> > languages.
> >
> > I can understand why Date and Random functions have be implemented via
> > monads because their output will always change.
> >
> > Thanks,
> > Shishir
> >
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> >
>
> --
> Jakob Holderbaum, M.Sc.
> Embedded Software Engineer
>
> 0176 637 297 71
> http://jakob.io/
> http://jakob.io/mentoring/
> hi at jakob.io
> @hldrbm
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150426/cf822dd2/attachment-0001.html>


More information about the Beginners mailing list