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

Adrian Adshead adrianadshead at yahoo.co.uk
Fri Jan 22 15:59:57 EST 2010


>All Haskell functions are pure without exception.  For example:
>
>greet :: String -> IO ()
>greet name = putStrLn $ "Hello, "++name
>
>This is a pure function from String to IO ().  This function (like all
>Haskell functions) has no side effects.  Its return value of type IO ()
>merely _represents_ an IO action.  The runtime system knows how to act
>on this representation.
>
>This also means that there is no such thing in Haskell as marking a
>function as side-effecting.
>
>This distinction may be subtle, but it's important.
>
>
>Steve

Steve,

Please could you clarify this for me since you are making exactly
the opposite assertion than I have understood.

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.

As I understand it the IO in the type signature is the programmers
indication to the compiler that the function is not guaranteed to
be side effect free.

add_pure :: Integer -> Integer
add_pure x = x + 5

add_impure :: Integer -> IO Integer
add_impure x = return (x + 5)

add_pure is clearly a pure function. add_impure while it is
totally side effect free and therefore fulfills the definition
of purity, is impure as far as the compiler is concerned since
I (the programmer) have told the compiler that I do not guarantee
that the function is pure.

Please let me know where I am misunderstanding purity.

Many thanks

Adrian.



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100122/55f48277/attachment.html


More information about the Beginners mailing list