[Haskell-beginners] Re: testing and the culture of Haskell

Brent Yorgey byorgey at seas.upenn.edu
Fri Jan 22 17:30:31 EST 2010


On Fri, Jan 22, 2010 at 08:59:57PM +0000, Adrian Adshead wrote:
>
> I am confused by you stating "All Haskell functions are pure
> without exception.".
> 
> Pure functions have no impact on 'anything'. They take input
> parameters (which they don't change) and return exactly the
> same result whenever the same input parameters are given.
> 
> >greet :: String -> IO ()
> >greet name = putStrLn $ "Hello, "++name
> 
> This example you gave is not a pure function since it does have
> the side effect that the screen is changed by outputting the string
> "Hello, " and the name passed in.

This is indeed a subtle point, but this example actually *is* a pure
function.  When you pass it a String, it returns a value of type IO
(), which is a *description* of a (side-effecting) computation to be
carried out.  Every time you pass it the same String, it will give you
back the exact same *description* of a computation.

It is only when that description is handed off to the runtime system
(usually by including it somewhere in the special IO () value called
'main') that it gets run and causes actual effects.

-Brent


More information about the Beginners mailing list