[More work on ch07 John Goerzen **20080108092740] { hunk ./en/ch07-io.xml 73 - is your key to knowing that they may have side-effects, or that + is your key to knowing that they may have side effects, or that hunk ./en/ch07-io.xml 128 - Performing an action of type IO + An expression may produce an action as its + value, but the action will not perform I/O until it is performed + + Performing (executing) an action of type IO hunk ./en/ch07-io.xml 142 - from an I/O action and store it in a variable. + from an I/O action and store it in a + variable.Or, more technically, it is used to + pull pure code into an I/O action. + hunk ./en/ch07-io.xml 148 - Finally, &main; itself is an I/O action with type IO - (). You can only execute I/O actions or use &larrow; from - within other I/O actions. So all I/O in Haskell programs is driven + &main; itself is an I/O action with type IO + (). You can only perform I/O actions from + within other I/O actions. All I/O in Haskell programs is driven hunk ./en/ch07-io.xml 153 - isolation from side-effects in Haskell: you perform I/O in + isolation from side effects in Haskell: you perform I/O in hunk ./en/ch07-io.xml 155 - from there. + from there. Most Haskell programs will still have the majority + of their code outside IO actions; the I/O + actions are just used to perform I/O and call the pure code. hunk ./en/ch07-io.xml 186 - has no side-effects, and it operates lazily. It uses other Haskell + has no side effects, and it operates lazily. It uses other Haskell hunk ./en/ch07-io.xml 188 - length. You can play with it in &ghci; just as you - would any other function. + length. hunk ./en/ch07-io.xml 197 - You can see here how we read from the keyboard the person's name. + You can see here how we read the person's name from the keyboard. hunk ./en/ch07-io.xml 199 - printed. In fact, the last line of &main; could have been replaced + printed. In fact, the last two lines of &main; could have been replaced hunk ./en/ch07-io.xml 201 - did have side-effects&emdash;it caused things to appear on the terminal, - for instance&emdash;name2reply did not and could not. + did have side effects&emdash;it caused things to appear on the terminal, + for instance&emdash;name2reply did not and + could not. That's because name2reply is pure + code, and not an action. hunk ./en/ch07-io.xml 210 + FIXME: have we introduced :l? + hunk ./en/ch07-io.xml 230 - is really imperative rather than lazy. It sure looks like a sequence - of actions to be followed in order. There's more to it than that, - though. We'll discuss that question later in this chapter in + is really imperative rather than pure, lazy, and functional. + Some of these examples look like a sequence + of actions to be followed in order. There's more to it than that, + though. We'll discuss that question later in this chapter in hunk ./en/ch07-io.xml 238 - - Haskell defines quite a few basic functions for I/O. The library - reference for System.IO provides a good summary of - them all, should you need one that we haven't demonstrated here. - + + Pure vs. Impure Code + + As a way to help with understanding the differences between + pure and impure code, here's a comparison table. When we + speak of pure code, we are talking about Haskell functions + that always return the same result when given the same input + and have no side effects. In Haskell, the only impure code is + the execution of I/O actions. + + + Pure vs. Impure Code + + + + + + Pure + Impure + + + + + Always produces the same result when given the + same parameters + May produce different results for the same + parameters + + + Never has side effects + May have side effects + + + May alter the global state of the program, + system, or world + Never alters state + + + +
+
+ + + + Why Purity Matters + + In this section, we've discussed how Haskell draws a clear + distinction between pure and impure code. Most languages + don't draw this distinction. In languages such as C or Java, + there is no such thing as a function that is guaranteed by the + compiler to + always return the same result for the same arguments, or a + function that is guaranteed to never have side effects. The + only way to know if a given function has side effects is to + read its documentation and hope that it's accurate. + + + Many bugs in programs are caused by unanticipated side + effects. Still more are caused by misunderstanding + circumstances in which functions may return different results + for the same input. As multithreadining and other forms of + parallelism becomt more and more common, it becomes more + difficult to manage global side effects. + + + Haskell's method of isolating side effects into I/O actions + provides a clear boundary. You can always know which parts of + the system may alter state and which won't. You can always be + sure that the pure parts of your program aren't having + unanticipated results. This helps you to think about the + program. It also helps the compiler to think about it. + Recent versions of &ghc;, for instance, can provide a level of + automatic parallelism for the pure parts of your code -- + something of a holy grail for computing. + + hunk ./en/ch07-io.xml 323 + + + Haskell defines quite a few basic functions for I/O. The library + reference for System.IO provides a good summary of + them all, should you need one that we aren't touching upon + here. Many of these are similar to functions you would see in + other programming languages. + FIXME: insert xref to instructions on finding library + ref + + hunk ./en/ch07-io.xml 349 - Let's start with an imperative way to read and write files. This will - probably seem familiar to a while loop that you may + Let's start with an imperative way to read and write files. + This should + seem similar to a while loop that you may hunk ./en/ch07-io.xml 834 - It's a pure function since it has no side-effects and always returns + It's a pure function since it has no side effects and always returns hunk ./en/ch07-io.xml 1265 - Side-Effects with Lazy I/O + Side Effects with Lazy I/O hunk ./en/ch07-io.xml 1271 - We need to get a bit more specific about what side-effects are. When + We need to get a bit more specific about what side effects are. When hunk ./en/ch07-io.xml 1281 - When we speak of no side-effects, we mean that pure code in Haskell - can't run commands that trigger side-effects. Pure functions + When we speak of no side effects, we mean that pure code in Haskell + can't run commands that trigger side effects. Pure functions }