[ch01 work John Goerzen **20070625064003] { hunk ./en/ch01-whyfp.xml 76 - - Looping with for or - while - FIXME - - hunk ./en/ch01-whyfp.xml 78 - FIXME + + In a language such as C, you might see a statement such as + x = x * 5 + y. This statement modifies the place + in memory where x is stored and changes it to + the result of the computation. Many, many other languages work the + same way. For instance, a Python programmer might say + mylist[5] = 10, which will set the value of the + fifth element in mylist to 10. + + + In Haskell, all data is immutable -- it cannot be modified. If you + need to modify an element of a list, you'll get a copy of the + original list returned -- except for the one changed element. Then + you will probably ignore the original list. + + + You might start to think that this could be slow to execute. + That turns out not to be the case, as the Haskell compiler can + optimize this sort of usage quite well. + + + It also turns out to be a powerful way to code: it makes it easy to + "chain" functions together, the result of one being taken as input to + another. POSIX programmers can think of this as similar to pipes in + the shell. + + + There's another benefit of this: a lack of side-effects. A common + source of bugs in many languages is calling a function that does + something unexpected: modifies a global or a class variable, for + instance. Later code then could behave in unexpected ways. + + + Some languages have partial immutability. For instance, strings are + immutable in Java; if you want to add something to a string in Java, + you combine two strings and the result is a new string -- not a + modification of an existing string. Haskell takes immutability much + farther. The only exception in Haskell deals with I/O, which is + encapsulated in a special way; this will be discussed later. + + + + + Looping with for or while + + Chances are that most languages you've used before have the notion + of for or while. These + tools are often used for performing actions a fixed number of times, + for doing something over and over until some state changes, or for + simply repeating infinitely until the program is killed. + + + Haskell has no for or while. + That's not an oversight; Haskell doesn't have for + or while because Haskell doesn't need them. + + hunk ./en/ch01-whyfp.xml 136 + }