<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">OK, so I get the idea that all functions are pure because they<br>just return actions.<br><br>So as I understand it now...<br><br>All functions are pure.<br>The evaluation of all functions not declared as IO results in no side effects.<br>The evaluation of all functions declared as IO may or may not have side effects.<br><br>But how can the function greetAdrian from the example below not be an IO<br>operation?<br><br><br><br><br>From: Maciej Piechotka <uzytkownik2 <at> gmail.com><br>
Subject: <a target="_top" rel="nofollow" href="http://news.gmane.org/find-root.php?message_id=%3c1264206701.5563.13.camel%40picard%3e">Re: Re: testing and the culture of Haskell</a><br>
Newsgroups: <a href="http://news.gmane.org/gmane.comp.lang.haskell.beginners" target="_top">gmane.comp.lang.haskell.beginners</a><br>
Date: 2010-01-23 00:31:42 GMT
(2 hours and 52 minutes ago)<br>
<pre>On Fri, 2010-01-22 at 20:59 +0000, Adrian Adshead wrote:<br>> >All Haskell functions are pure without exception. For example:<br>> ><br>> >greet :: String -> IO ()<br>> >greet name = putStrLn $ "Hello, "++name<br>> ><br>> >This is a pure function from String to IO (). This function (like all<br>> >Haskell functions) has no side effects. Its return value of type IO ()<br>> >merely _<u>represents</u>_ an IO action. The runtime system knows how to act<br>> >on this representation.<br>> ><br>> >This also means that there is no such thing in Haskell as marking a<br>> >function as side-effecting.<br>> ><br>> >This distinction may be subtle, but it's important.<br>> ><br>> ><br>> >Steve<br>> <br>> Steve,<br>> <br>> Please could you clarify this for me since you are making exactly<br>> the opposite assertion than I have
understood.<br>> <br>> I am confused by you stating "All Haskell functions are pure<br>> without<br>> exception.".<br>> <br>> Pure functions have no impact on 'anything'. They take input<br>> parameters (which they don't change) and return exactly the<br>> same result whenever the same input parameters are given.<br>> <br>> >greet :: String -> IO ()<br>> >greet name = putStrLn $ "Hello, "++name<br>> <br>> This example you gave is not a pure function since it does have<br>> the side effect that the screen is changed by outputting the string<br>> "Hello, " and the name passed in.<br>> <br>> <br><br>greatAdrian :: String<br>greetAdrian = let x = greet "Adrian"<br> in x `seq` f x<br><br>greet can be consider a pure function and value IO () is evaluated by<br>seq. IO () represents an action(s) not execution of action(s). If f of x<br>does not use any tricks nothing will be
printed.<br><br>IO a value it can be:<br>- cast unsafely into a. However I guess we omit this shame for a moment<br>- binded with other action. But the resultant type is now again IO b. So<br>we still get a something <br>- returned as main. Then we might consider whole Haskell program as<br>metalanguage which returns single thing - other program. In similar way<br>as:<br><br>type PythonProgram = String<br>main :: PythonProgram<br>main = "print \"Hello World\""<br><br>is pure add_impure is pure. What we do with the result is other thing.<br><br>Regards<br><br></pre></td></tr></table><br>