[Many edits to chapter 3. Bryan O'Sullivan **20070919183355] { move ./examples/ch03/Enum.hs ./examples/ch03/SimpleTypes.hs hunk ./en/Makefile 27 + $(wildcard ../examples/ch[0-9][0-9]/*.c) \ + $(wildcard ../examples/ch[0-9][0-9]/*.cpp) \ hunk ./en/Makefile 31 - $(wildcard ../examples/ch[0-9][0-9]/*/*.hs) \ - $(wildcard ../examples/ch04/*.c) \ - $(wildcard ../examples/ch04/*.cpp) \ - $(wildcard ../examples/ch04/*.java) \ - $(wildcard ../examples/ch04/*.py) + $(wildcard ../examples/ch[0-9][0-9]/*.java) \ + $(wildcard ../examples/ch[0-9][0-9]/*.py) \ + $(wildcard ../examples/ch[0-9][0-9]/*/*.hs) hunk ./en/ch03-funcs-types.xml 97 - Int is usually 32 bits wide, while on a 64-bit - machine, it is usually 64 bits wide. + Int is usually 31 bits wide, while on a 64-bit + machine, it is usually 63 bits wide. (There are also + numeric types that are exactly 8, 16, and so on bits wide; + we'll get to those later.) hunk ./en/ch03-funcs-types.xml 105 - used as often as Ints, because they're a lot + used as often as Ints, because they're hunk ./en/ch03-funcs-types.xml 111 - wide, and uses the machine's native floating point + wide, and uses the system's native floating point hunk ./en/ch03-funcs-types.xml 115 - efficient.) + efficient, so Float is much slower.) hunk ./en/ch03-funcs-types.xml 130 - written [Char]. + written [Char]. (The type String is + simply an alias for [Char]. You can use the two + interchangeably.) hunk ./en/ch03-funcs-types.xml 182 + A handy rule of thumb for remembering which is which is that + a list has variable size and elements of uniform type, while a + tuple has a uniform size and elements of variable type. + hunk ./en/ch03-funcs-types.xml 423 - returns an empty list if the number to remove is greater than + returns the original list if the number to remove is greater than hunk ./en/ch03-funcs-types.xml 431 + The then arm of the if + expression returns the input list once either the number of + elements to remove reaches zero or the current list is empty. + The else arm calls myTake + recursively, decrementing the counter and stripping one + element from the front of the list. The conditional clause + inside the if causes the else branch + to be called repeatedly until either the remaining list is + empty or there are no elements left to return. + hunk ./en/ch03-funcs-types.xml 581 + Deriving what? + hunk ./en/ch03-funcs-types.xml 604 + hunk ./en/ch03-funcs-types.xml 610 + A constructor serves as both a function for creating a value + and a tag identifying what type of value we have. + The value that &ghci; prints is telling us that we have created + a value with the tag MyConstructor, with the given + values in each slot. (By the way, this is why we + had to add deriving (Show) to the definition of our + type; without that, &ghci; would print an error message, telling + us that it doesn't know how to print a value of this + type.) + hunk ./en/ch03-funcs-types.xml 625 + The &ghci; command :type lets us see what + the type of that expression is. + + &mytype.ghci:valueType; + hunk ./en/ch03-funcs-types.xml 657 + The use of the word algebraic simply + indicates that the components of an algebraic data type are used + together. + hunk ./en/ch03-funcs-types.xml 669 - Each constructor can take zero or more arguments; the - numbers and types of the arguments accepted by each constructor - are independent. For example, here's one way we might represent - versions of the Windows operating system, where old releases - were monolithic, and newer releases have service pack - levels denoting major updates after their initial - releases. + Each constructor of an algebraic type can take + zero or more arguments; the numbers and types of the arguments + accepted by each constructor are independent. For example, + here's one way we might represent versions of the Windows + operating system, where old releases were monolithic, and newer + releases have service pack levels denoting major + updates after their initial releases. hunk ./en/ch03-funcs-types.xml 696 - struct in C or C++, and its components to the - fields of a struct. + struct in C or C++, and its components correspond to the + fields of a struct. Here's such a struct. + + &types.c:coord; + + And here's an equivalent type in Haskell. + + &SimpleTypes.hs:Coord; + + The only significant difference is that the fields in the + Haskell type are anonymous and positional. In , we'll see how to use names + to access the fields of the Coord type. hunk ./en/ch03-funcs-types.xml 723 - Algebraic data types also serve where we'd use an - enum in C or C++, to represent a range of - discrete symbolic values. + Algebraic data types also serve where we'd use + an enum in C or C++, to represent a range of + discrete symbolic values. Such algebraic data types are + sometimes referred to as enumeration types. Here's an example + from C. + + &types.c:roygbiv; hunk ./en/ch03-funcs-types.xml 731 - &Enum.hs:Roygbiv; + And here's a Haskell equivalent. + + &SimpleTypes.hs:Roygbiv; hunk ./en/ch03-funcs-types.xml 1236 + And for good measure, here's how we can define functions to + access the fields of the Coord type we defined in + . + + &SimpleTypes.hs:accessors; + hunk ./en/ch03-funcs-types.xml 1306 + + Wild cards also help readability, as they make it easier + to tell which values we're really using. + + &SimpleTypes.hs:niceAccessors; + hunk ./examples/ch03/SimpleTypes.hs 1 +{-- snippet Coord --} +data Coord = Coord Int Int +{-- /snippet Coord --} + +{-- snippet accessors --} +coordX (Coord x y) = x +coordY (Coord x y) = y +{-- /snippet accessors --} + +{-- snippet niceAccessors --} +nicerX (Coord x _) = x +nicerY (Coord _ y) = y +{-- /snippet niceAccessors --} + hunk ./examples/ch03/SimpleTypes.hs 25 --- Equivalent in C or C++: --- --- enum roygbiv { red, orange, green, blue, indigo, violet }; - hunk ./examples/ch03/mytype.ghci 11 + +--# valueType + +:type MyConstructor 1 "foo" addfile ./examples/ch03/types.c hunk ./examples/ch03/types.c 1 +/** snippet coord */ +struct address { + int x; + int y; +}; +/** /snippet coord */ + +/** snippet roygbiv */ +enum roygbiv { + red, + orange, + yellow, + greeen, + blue, + indigo, + violet, +}; +/** /snippet roygbiv */ }