closures with side effects

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

closures with side effects

tpledger
dkarapet wrote:
> I have been trying to understand closures
> in haskell and how they relate
> to side effects. I have been looking
> around but all I find are trivial
> examples with no side effects. Please let
> me know if you know of any examples.

The side effects occur in the context that causes the
closure to be entered.

Here's a nigh-trivial example.

    myClosure :: IO ()
    myClosure = putStrLn "Hello, world."
    main      = myClosure >> myClosure

When myClosure is defined, the side effect doesn't occur
yet.  We just have a *definition* of an IO action that
hasn't yet been bound into the program's sequence of
actions.

When main binds myClosure into the program's sequence of
actions (twice), the side effect occurs (twice).  Depending
on the implementation of putStrLn, it may be faster the
second time because the same closure has been entered
before.

HTH,
Tom
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe