[Haskell-beginners] [Haskell-cafe] select :: [(Float, a)] -> a -- Weighted stochastic selection - help?

Tony Hannan tonyhannan2 at gmail.com
Sat Sep 6 10:41:07 EDT 2008


The IO monad is defined as:

newtype IO a = IO (RealWorld -> (RealWorld, a))

Notice that it encapsulates a pure function from RealWorld to RealWord plus
result. Semantically, the whole state of the world is passed into your main
function and a new world state is returned containing any changes you made.
However, the implementation just performs side effects on the world (I/O),
but the monad type ensures that these side effects are sequential and that
you can never gain access to the underlying RealWorld so it won't be
duplicated. These restrictions allow us to view this implementation as a
pure function.

Non-primitive monads like Control.Monad.State are actually implemented as
pure functions passing the state around.

Cheers,
Tony

On Sat, Sep 6, 2008 at 10:18 AM, apfelmus <apfelmus at quantentunnel.de> wrote:

> C.M.Brown wrote:
> >> Technically, even the IO monad is pure, that's just the runtime-system
> >> > that consume your 'main' function that perform effects (and
> unsafeP...).
> >
> > But, sure the IO monad does have side-effects? I'm confused as to how it
> > could be pure. Could you explain?
>
> > I'm sorry, but I just don't get it. What are you trying to say? I think
> it
> > would be clearer if we started to define what exactly a "side-effect" is
> > (in any language) and work our definitions from there, because now I'm
> > really confused.
>
> A function say  f :: Int -> Int  is said to be *pure* if its result depends
> only
> on the argument.
>
> In Haskell, all functions are pure and the IO monad doesn't change anything
> about that. This is what is meant by "IO is pure". While IO allows you to
> print
> stuff on the screen, it doesn't change the fundamental property of the
> language
> that all functions are free of side-effects.
>
> In other languages like C or even ML, there are functions like
>
>  putStr  :: String -> ()
>  getChar :: () -> Char
>
> whose results depend on more things than their parameter and who can
> perform
> side-effects.
>
>
> Regards,
> apfelmus
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20080906/dc8b3b70/attachment.htm


More information about the Beginners mailing list