[More rewriting of chapter 2. I think (hope!) all is now accounted for. Bryan O'Sullivan **20071010070645] { hunk ./en/ch02-starting.xml 6 + As you read this chapter and the few that follow, it's good to + keep in mind that early on, we will sometimes introduce ideas in + restricted, simplified form. Haskell is a deep language, and + presenting every aspect of a given subject all at once is likely + to prove overwhelming. As we build a solid foundation in Haskell, + we'll expand upon these initial explanations. + hunk ./en/ch02-starting.xml 313 + + Remain dauntless in the face of error messages + + An important guideline here is one that we'll + be repeating over and over throughout the early sections of + this book. If you run into problems or error messages that + you do not yet understand, don't panic. + Early on, all you have to do is figure out enough to make + progress through a problem. As you acquire experience, parts + of error messages that now seem obscure will become easier + to follow. + + The numerous error messages have a purpose: they + actually help us in writing correct code, by making us + perform some amount of debugging up front, + before we ever run a program. If you are coming from a + background of working with more permissive languages, this + way of working may come as something of a shock. Bear with + us. + + hunk ./en/ch02-starting.xml 349 + Also, where C and Perl use ! for + logical negation, Haskell uses the not + function. + + &basics.ghci:not; + hunk ./en/ch02-starting.xml 425 - Using the &let; construct, we can define - e ourselves. + Using &ghci;'s &let; construct, we can make a + temporary definition of e ourselves. hunk ./en/ch02-starting.xml 430 - We can then use our definition of e in - arithmetic expressions. + This is a call to the base-e + exponential function, exp, and our first + example of calling a function in Haskell. While languages + like Python require parentheses around the arguments to a + function, Haskell does not. + + With e defined, we can use it + in arithmetic expressions. hunk ./en/ch02-starting.xml 486 - - Don't sweat those error messages - - Remember the point we made earlier about explicit - parentheses often being good? Here's an example of that idea in - action, except here the parentheses are - necessary, not merely a good idea. - - &basics.ghci:denom.bad; - - Look at that error message; it's detailed, and - mentions a lot of concepts we haven't covered yet. The best way - to start dealing with it is to not worry early on about the - details. The error messages that &GHC; produces can be daunting - to newcomers, but take heart. As you become more familiar with - Haskell, you'll acquire enough understanding to interpret error - messages more easily. - - For now, however, we must make some kind of progress. - If we step back and gloss over the error message, we can see - that &ghci; is displaying for us the expression that it had - trouble with. And it has helpfully added parentheses back into - the expression, only look! They're in the wrong - places. - - This isn't the fault of &ghci;. It just happens that the - (%) and (*) operators - have the same precedences, so &ghci; parses the expression from - left to right. As a result, it tries to construct a rational - from another rational and an integer, instead of from a pair of - integers. (If (%) had a higher precedence - than (*), the expression would be parsed - the way we might have hoped.) - - All we have to do to correct this problem is add some - explicit parentheses to say what we really mean. - - &basics.ghci:denom.good; - - - Remain dauntless in the face of error messages - - The important lesson here is one that we'll be repeating - over and over throughout the early sections of this book. If - you run into problems, don't panic. You - do not need to understand every little detail of what's going - on. Early on, all you have to do is figure out enough to make - progress through a problem. You don't need instantaneous - complete understanding, so don't worry if you don't have - it. It will come. - - The numerous error messages have a purpose: they actually - help us to write correct code, by making us perform some - amount of debugging up front, before we ever - run a program. If you are coming from a background of working - with more permissive languages, this way of working may come - as something of a shock. Bear with us. - - - - hunk ./en/ch02-starting.xml 514 - into a number, so the list expression isn't properly typed. + into a Boolean value, so the list expression isn't properly + typed. hunk ./en/ch02-starting.xml 517 - We can write a series of elements using range - notation, and Haskell will fill in the contents of the list for - us. + If we write a series of elements using + range notation, Haskell will fill in the + contents of the list for us. hunk ./en/ch02-starting.xml 523 - Here, the .. characters denote a - range. We can only use range notation for types we can count. - Range notation makes no sense for rational numbers, for example, - because there is not a countable number of rationals. + Here, the .. characters denote a range. + We can only use range notation for types whose elements we can + enumerate. Range notation makes no sense for rational numbers, + for example, because there is not a countable number of + rationals. hunk ./en/ch02-starting.xml 544 - We can omit the end point of a range. If the type of the - range has an upper bound on its values (or a lower bound if the - step is negative), that bound will be the end point of the - range. Otherwise, the range will continue indefinitely. + We can omit the end point of a range. If the type we're + enumerating has an upper bound on its values (or a lower bound + if the step is negative), that bound will be the end point of + the range. Otherwise, the range will continue indefinitely. + For example, if you type [1..] at the &ghci; + prompt, you'll have to interrupt or kill &ghci; to get it to + stop printing an infinite succession of ever-larger + numbers. hunk ./en/ch02-starting.xml 642 - read everything. + follow the names. hunk ./en/ch02-starting.xml 695 - Couple it with liberal use of the - arrow keys to recall and edit the last expression we typed, - and we have a fairly decent environment for interactive - experiments, where the cost of mistakes is very low. Take - advantage of the opportunity to make cheap, plentiful mistakes - when you're exploring the language! + When we couple it with + liberal use of the arrow keys to recall and edit the last + expression we typed, and this gives us a fairly decent + environment for interactive experiments, where the cost of + mistakes is very low. Take advantage of the opportunity to + make cheap, plentiful mistakes when you're exploring the + language! hunk ./en/ch02-starting.xml 709 - Haskell's unbounded integral type is named - Integer . + Haskell's integer type is named + Integer, and is unbounded; only your system's + memory capacity limits the size of the integers it can + represent. hunk ./en/ch02-starting.xml 773 + hunk ./examples/ch02/basics.ghci 5 -7/2 +7.0 / 2.0 hunk ./examples/ch02/basics.ghci 45 -let e = 2.7182818284590451 +let e = exp 1 hunk ./examples/ch02/basics.ghci 70 +--# not + +not True + hunk ./examples/ch02/basics.ghci 93 ---# ratio - -7 % 2 - ---# ratarith - -7 % 2 + 2 % 3 - ---# denom - -(22 % 7) * 7 - hunk ./examples/ch02/basics.ghci 154 -[1, 2, "buckle my shoe"] +[True, False, "testing"] }