[Baby steps with types. Bryan O'Sullivan **20070710053216] { hunk ./en/ch02-starting.xml 688 - - Haskell Programs: Source Files and Functions - FIXME: What Haskell source files look like. How to write a simple - function. Hey, functions look different from traditional languages! - How to get ghci to load the source file; using the definitions from - it. - + + First steps with types + + Despite all our talk of types, our interactions with &ghci; + have so far been free of much type-related thinking. We haven't + told &ghci; what types we've been using, and it's mostly been + willing to accept our input. + + The first thing we can do to start explicitly exploring the + world of types is to get &ghci; to tell us more about what it's + doing. &ghci; has a command, :set, that lets + us change a few of its default behaviours. We can tell it to + print more type information as follows. + + &types.ghci:set_t_on; + + What the +t does is tell &ghci; to print the + type of an expression after the expression. That cryptic + it in the output can be very useful: it's + actually the name of a special variable, in which &ghci; stores + the result of the last expression we evaluated. Let's break + down the meaning of the last line of &ghci; output, then. + + + + It's telling us about the variable + it. + + + We can read the text :: as meaning + the value on the left has the type on the + right. + + + And the value on the right is the type in question, + namely [Char]. + + + + + The joy of <quote>it</quote> + + That it variable is wonderfully handy + when we want to use the result of a prior expression in a new + expression. + + &types.ghci:it; + + When evaluating an expression, &ghci; won't change the + value of it until after it's finished. An + error in the evaluation will leave it + untouched, so we can type possibly bogus expressions with a + bit of a safety net. + + &types.ghci:it.bad; + + Coupling 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! + + + Here are a few more of Haskell's names for types, from + expressions of the sort we've already seen. + + &types.ghci:integer; + + Integer is Haskell's arbitrary-precision + integer type. + + + Both of the type names we've seen so far, + Char and Integer, have started with + capital letters. This isn't an accident; Haskell requires + type names to start with an uppercase letter, and variable + names with a lowercase letter. + + + Rational numbers don't look quite the same. + + &types.ghci:ratio; + + Notice two words on the right hand side + of the :: above. We can read this as a + Ratio of Integer, and infer that a + Ratio will probably need to have values of type + Integer as both numerator and denominator. Sure + enough, if we try to construct a Ratio where the + numerator and denominator are of different types, &ghci; + complains. + + &types.ghci:ratio.bad; + + Although it's initially useful to have :set + +t giving us type information for every expression + we enter, this is a facility we'll quickly outgrow, simply + because it will become redundant: after a while, we'll often + know what type we expect an expression to have, so why get + &ghci; to remind us? We can turn off the extra type information + at any time. + + &types.ghci:set_t_off; + + Even with this facility turned off, we can still get that + type information easily when we need it, using another &ghci; + command. + + &types.ghci:type; + + The :type command will print type + information for any expression we give it (including + it, as we see above). It won't actually + evaluate the expression; it only checks its type and prints + that. + + + Exercises + + + + + Foo? + + + + hunk ./en/ch02-starting.xml 818 - - Types - FIXME: - Introduce types. Use ghci to inspect the types of a few values and - functions. Describe what the "->" means. Note the difference in - case between the first character of types and functions/values. - Take the type information ghci gave us; put it in the source file; - reload. Notice that ghci is still happy. - + + Conclusion + + In this chapter, we've seen how to install &GHC; for our + environment. We then went on to interactively explore some + basic Haskell concepts using &ghci; the interpreter bundled with + &GHC;. We saw how to evaluate simple expressions. + + We dipped a toe into the water of typing. + We also had drilled into us the notion that we don't need to + understand, or be intimidated by, every single gripe that &ghci; + throws our way. We'll do just fine with a shallow understanding + initially, and rely on practice and more reading to round out + our knowledge. Onwards! + }