[Haskell-cafe] What are side effects in Haskell?
Conal Elliott
conal at conal.net
Tue Dec 30 02:35:29 EST 2008
>
> Everything in Haskell is a function [...]
Where did this idea come from?
I'd say every expression in Haskell denotes a pure value, only some of which
are functions (have type a->b for some types a & b).
- Conal
On Sat, Dec 27, 2008 at 12:16 PM, Cristiano Paris <frodo at theshire.org>wrote:
> On Sat, Dec 27, 2008 at 4:50 PM, Hans van Thiel <hthiel.char at zonnet.nl>
> wrote:
>
> > However, some functions in Haskell may have side effects, like printing
> > something on the screen, updating a database, or producing a random
> > number. These functions are called 'actions' in Haskell.
>
> No :D I'll try to explain using the same picture that came into my
> mind when I first got what monads really are...
>
> Everything in Haskell is a function AND an Haskell function is always
> pure, i.e. has no side effects.
>
> Functions may return "actions", which are a special kind of value even
> though they are no different to Haskell from values of any other type.
>
> When you are returning an action is like when you go to a driver and
> give him a car: the driver takes the car you gave him and takes it
> wherever the car is supposed to go. You build the car using small
> pieces (i.e. basic functions returning actions like putStrLn) and
> putting everything together using combinators (like >>= and >> or the
> "do" notation).
>
> So, the car in the example is an "action" (or, more formally, a value
> in a monad). Your Haskell functions (like putStrLn or anything) just
> return the car, but they don't run it: they are run at run-time by the
> Haskell run-time environment whose only task is to run values in the
> IO Monad, i.e. running the car.
>
> Even it may not be apparent for the IO Monad, every Monad value is run
> at some point in time: almost every monad is associated to at least a
> function running the monad: for the State Monad this is runState, for
> the Reader Monad is runReader and so on.
>
> The only exception to this is the IO Moand, which can be run only by
> the Haskell run time, implicitly: this is the reason why you'll never
> see a "runIO" thing.
>
> When the monad is run, it generate side-effects, like printing to
> screen, opening file and so on.
>
> If you're used to Unix administration, another image to understand
> monads are scripts. Haskell functions return script files that are run
> by the shell to affect the system they are run on. In the end, they
> are small pieces (the basic commands present in the system) put
> together by bash constructs, like "for", "while" and so on.
>
> Hope this helps.
>
> --
> Cristiano
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081229/b56d5517/attachment.htm
More information about the Haskell-Cafe
mailing list