| 1 | # This is a transcription of Hal Daume III's "Yet Another Haskell Tutorial" |
|---|
| 2 | # The original is 198 PDF pages which is hard to read. |
|---|
| 3 | # This copy is currently incomplete. |
|---|
| 4 | # Original is available at http://www.isi.edu/~hdaume/htut/ |
|---|
| 5 | |
|---|
| 6 | = About This Report |
|---|
| 7 | |
|---|
| 8 | The goal of the /Yet Another Haskell Tutorial/ is to provide a complete |
|---|
| 9 | intoduction to the Haskell programming language. It assumes no knowledge |
|---|
| 10 | of the Haskell language or familiarity with functional programming in |
|---|
| 11 | general. However, general familiarity with programming concepts (such as |
|---|
| 12 | algorithms) will be helpful. This is not intended to be an introduction |
|---|
| 13 | to programming in general; rather, to programming in Haskell. Sufficient |
|---|
| 14 | familiarity with your operating system and a text editor is also |
|---|
| 15 | necessary (this report only discusses installation on configuration on |
|---|
| 16 | Windows and *Nix system; other operating systems may be supported -- |
|---|
| 17 | consult the documentation of your chosen compiler for more information |
|---|
| 18 | on installing on other platforms). |
|---|
| 19 | |
|---|
| 20 | == What is Haskell? |
|---|
| 21 | |
|---|
| 22 | Haskell is called a lazy, pure functional programming language. It is |
|---|
| 23 | called /lazy/ because expressions which are not needed to determine the |
|---|
| 24 | answer to a problem are not evaluated. The opposize of lazy is /strict/, |
|---|
| 25 | which is the evaluation strategry of most common programming languages |
|---|
| 26 | (C, C++, Java, even ML). A strict language is one in which every |
|---|
| 27 | expression is evaluated, whether the result of its computation is |
|---|
| 28 | important or not. (This is probably not entirely true as optimizing |
|---|
| 29 | compilers for strict languages often do what's called "dead code |
|---|
| 30 | elmination" -- this removes unused expressions from the program.) It |
|---|
| 31 | is called /pure/ because it does not allow side effects (A side effect is |
|---|
| 32 | something that affects the "state" of the world. For instance, a |
|---|
| 33 | function that prints something to the screen is said to be side- |
|---|
| 34 | effecting, as is a function which affects the value of a global |
|---|
| 35 | variable.) -- of course, a programming language without side effects |
|---|
| 36 | would be horribly useless; Haskell uses a system of /monads/ to isolate |
|---|
| 37 | all impure computations from the rest of the program and perform them in |
|---|
| 38 | the safe way (see Chapter 9 for a discussion of monads proper or Chapter |
|---|
| 39 | 5 for how to do input/output in a pure language). |
|---|
| 40 | |
|---|
| 41 | Haskell is called a /functional/ language because the evaluation of a |
|---|
| 42 | program is equivalent to evaluating a function in the pure mathematical |
|---|
| 43 | sense. This also differs from standard languages (like C and Java) which |
|---|
| 44 | evaluate a sequence of statements, one after the other (this is termed |
|---|
| 45 | an /imperative/ langauge). |
|---|
| 46 | |
|---|
| 47 | The History of Haskell The history of Haskell is best described using the |
|---|
| 48 | words of the authors. The following text is quoted from the published version |
|---|
| 49 | of the Haskell 98 Report: |
|---|
| 50 | |
|---|
| 51 | .indent |
|---|
| 52 | In September of 1987 a meeting was held at the conference on Functional |
|---|
| 53 | Programming Languages and Computer Architecture (FPCA '87) in Portland, |
|---|
| 54 | Oregon, to discuss an unfortunate situation in the functional |
|---|
| 55 | programming community: there had come into being more than a dozen |
|---|
| 56 | nonstrict, purely functional programming languages, all similar in |
|---|
| 57 | expressive power and semantic underpinnings. There was a strong |
|---|
| 58 | consensus at this meeting that more widespread use of this class of |
|---|
| 59 | functional languages was being hampered by the lack of a common |
|---|
| 60 | language. It was decided that a committee should be formed to design |
|---|
| 61 | such a language, providing faster communication of new ideas, a stable |
|---|
| 62 | foundation for real applications development, and a vehicle through |
|---|
| 63 | which others would be encouraged to use functional languages. This |
|---|
| 64 | document describes the result of that committee's efforts: a purely |
|---|
| 65 | functional programming language called Haskell, named after the logician |
|---|
| 66 | Haskell B. Curry whose work provides the logical basis for much of ours. |
|---|
| 67 | |
|---|
| 68 | The committee's primary goal was to design a language that satisfied |
|---|
| 69 | these constraints: |
|---|
| 70 | + It should be suitable for teaching, research, and applications, |
|---|
| 71 | including building large systems. |
|---|
| 72 | + It should be completely described via the publication of a formal |
|---|
| 73 | syntax and semantics. |
|---|
| 74 | + It should be freely available. Anyone should be permitted to implement |
|---|
| 75 | the language and distribute it to whomever they please. |
|---|
| 76 | + It should be based on ideas that enjoy a wide consensus. |
|---|
| 77 | + It should reduce unnecessary diversity in functional programming |
|---|
| 78 | languages. |
|---|
| 79 | |
|---|
| 80 | The committee intended that Haskell would serve as a basis for future |
|---|
| 81 | research in language design, and hoped that extensions or variants of |
|---|
| 82 | the language would appear, incorporating experimental features. |
|---|
| 83 | |
|---|
| 84 | Haskell has indeed evolved continuously since its original publication. |
|---|
| 85 | By the middle of 1997, there had been four iterations of the language |
|---|
| 86 | design (the latest at that point being Haskell 1.4). At the 1997 Haskell |
|---|
| 87 | Workshop in Amsterdam, it was decided that a stable variant of Haskell |
|---|
| 88 | was needed; this stable language is the subject of this Report, and is |
|---|
| 89 | called "Haskell 98". |
|---|
| 90 | |
|---|
| 91 | Haskell 98 was conceived as a relatively minor tidy-up of Haskell 1.4, |
|---|
| 92 | making some simplifications, and removing some pitfalls for the unwary. |
|---|
| 93 | |
|---|
| 94 | It is intended to be a "stable" language in sense the /implementors are |
|---|
| 95 | committed to supporting Haskell 98 exactly as specified, for the |
|---|
| 96 | foreseeable future/. |
|---|
| 97 | |
|---|
| 98 | The original Haskell Report covered only the language, together with a |
|---|
| 99 | standard library called the Prelude. By the time Haskell 98 was |
|---|
| 100 | stabilised, it had become clear that many programs need access to a |
|---|
| 101 | larger set of library functions (notably concerning input/output and |
|---|
| 102 | simple interaction with the operating system). If these program were to |
|---|
| 103 | be portable, a set of libraries would have to be standardised too. A |
|---|
| 104 | separate effort was therefore begun by a distinct (but overlapping) |
|---|
| 105 | committee to fix the Haskell 98 Libraries. |
|---|
| 106 | .indent. |
|---|
| 107 | |
|---|
| 108 | == Why Use Haskell? |
|---|
| 109 | |
|---|
| 110 | Clearly you're interested in Haskell since you're reading this tutorial. |
|---|
| 111 | There are many motivations for using Haskell. My personal reason for |
|---|
| 112 | using Haskell is that I have found that I write more bug-free code in |
|---|
| 113 | less time using Haskell than any other language. I also find it very |
|---|
| 114 | readable and extensible. |
|---|
| 115 | |
|---|
| 116 | Perhaps most importantly, however, I have consistently found the Haskell |
|---|
| 117 | community to be incredibly helpful. The language is constantly evolving |
|---|
| 118 | (that's not to say it's instable; rather that there are numerous |
|---|
| 119 | extensions that have been added to some compilers which I find very |
|---|
| 120 | useful) and user suggestions are often heeded when new extensions are to |
|---|
| 121 | be implemented. |
|---|
| 122 | |
|---|
| 123 | == Why Not Use Haskell? |
|---|
| 124 | |
|---|
| 125 | My two biggest complaints, and the complaints of most Haskellers I know, |
|---|
| 126 | are: (1) the generated code tends to be slower than equivalent programs |
|---|
| 127 | written in a language like C; and (2) it tends to be difficult to debug. |
|---|
| 128 | |
|---|
| 129 | The second problem tends not be to a very big issue: most of the code |
|---|
| 130 | I've written is not buggy, as most of the common sources of bugs in |
|---|
| 131 | other languages simply don't exist in Haskell. The first issue certainly |
|---|
| 132 | has come up a few times in my experience; however, CPU time is almost |
|---|
| 133 | always cheaper than programmer time and if I have to wait a little |
|---|
| 134 | longer for my results after having saved a few days programming and |
|---|
| 135 | debugging. |
|---|
| 136 | |
|---|
| 137 | Of course, this isn't the case of all applications. Some people may find |
|---|
| 138 | that the speed hit taken for using Haskell is unbearable. However, |
|---|
| 139 | Haskell has a standardized /foreign-function interface/ which allow you |
|---|
| 140 | to link in code written in other languages, for when you need to get the |
|---|
| 141 | most speed out of your code. If you don't find this sufficient, I would |
|---|
| 142 | suggest taking a look at the language O'Caml, which often /outperforms/ |
|---|
| 143 | even C++, yet also has many of the benefits of Haskell. |
|---|
| 144 | |
|---|
| 145 | == Target Audience |
|---|
| 146 | |
|---|
| 147 | There have been many books and tutorials written about Haskell; for a |
|---|
| 148 | (nearly) complete list, visit the http://haskell.org/bookshelf (Haskell |
|---|
| 149 | Bookshelf) at the Haskell homepage. A brief survey of the tutorials |
|---|
| 150 | available yields: |
|---|
| 151 | |
|---|
| 152 | * /A Gentle Introduction to Haskell/ is an introduction to |
|---|
| 153 | Haskell, given that the reader is familiar with functional |
|---|
| 154 | programming en large. |
|---|
| 155 | * /Haskell Companion/ is a short reference of common concepts and |
|---|
| 156 | definitions. |
|---|
| 157 | * /Online Haskell Course/ is a short course (in German) for beginning |
|---|
| 158 | with Haskell. |
|---|
| 159 | * /Two Dozen Short Lessons in Haskell/ is the draft of an excellent |
|---|
| 160 | textbook that emphasizes user involvement. |
|---|
| 161 | * /Haskell Tutorial/ is based on a course given at the 3rd International |
|---|
| 162 | Summer School on Advanced Functional Programming. |
|---|
| 163 | * /Haskell for Miranda Programmers/ assumes knowledge of the |
|---|
| 164 | language Miranda. |
|---|
| 165 | * /PLEAC-Haskell/ is a tutorial in the style of the Perl Cookbook. |
|---|
| 166 | |
|---|
| 167 | Though all of these tutorials is excellent, they are on their own |
|---|
| 168 | incomplete: The "Gentle Introduction" is far too advanced for beginning |
|---|
| 169 | Haskellers and the others tend to end too early, or not cover |
|---|
| 170 | everything. Haskell is full of pitfalls for new programmers and |
|---|
| 171 | experienced non-functional programmers alike, as can be witnessed by |
|---|
| 172 | reading through the archives of the Haskell mailing list. |
|---|
| 173 | |
|---|
| 174 | It became clear that there is a strong need for a tutorial which is |
|---|
| 175 | introductory in the sense that it does not assume knowledge of |
|---|
| 176 | functional programming, but which is advanced in the sense that it |
|---|
| 177 | /does/ assume some background in programming. Moreover, none of the |
|---|
| 178 | known tutorials introduce input/output and iteractivity soon enough (not |
|---|
| 179 | even until the 248th page, as in the case of the Hudak book). This |
|---|
| 180 | tutorial is not for beginning programmers; some experience and knowledge |
|---|
| 181 | of programming and computers is assumed (though the appendix does |
|---|
| 182 | contain some background information). |
|---|
| 183 | |
|---|
| 184 | The Haskell language underwent a standardization process and the result |
|---|
| 185 | is called Haskell 98. The majority of this book will cover the Haskell |
|---|
| 186 | 98 standard. Any deviations from the standard will be noted (for |
|---|
| 187 | instance, many compilers offer certain extensions to the standard which |
|---|
| 188 | are useful; some of these may be discussed). The goals of this tutorial |
|---|
| 189 | are: |
|---|
| 190 | |
|---|
| 191 | * to be /practical/ above all else |
|---|
| 192 | * to provide a comprehensive, free introduction to the Haskell language |
|---|
| 193 | * to point out common pitfalls and their solutions |
|---|
| 194 | * to provide a good sense of how Haskell can be used in the real world |
|---|
| 195 | |
|---|
| 196 | == Acknowledgements |
|---|
| 197 | |
|---|
| 198 | It would be inappropriate not to give credit also to the |
|---|
| 199 | original designers of Haskell. Those are: Arvind, Lennart Augustsson, Dave |
|---|
| 200 | Barton, Brian Boutel, Warren Burton, Jon Fairbairn, Joseph Fasel, Andy Gordon, |
|---|
| 201 | Maria Guzman, Kevin Hammond, Ralf Hinze, Paul Hudak, John Hughes, Thomas |
|---|
| 202 | Johnsson, Mark Jones, Dick Kieburtz, John Launchbury, Erik Meijer, Rishiyur |
|---|
| 203 | Nikhil, John Peterson, Simon Peyton Jones, Mike Reeve, Alastair Reid, Colin |
|---|
| 204 | Runciman, Philip Wadler, David Wise, Jonathan Young. |
|---|
| 205 | |
|---|
| 206 | Finally, I would like to specifically thank Simon Peyton Jones, Simon |
|---|
| 207 | Marlow, John Hughes, Alastair Reid, Koen Classen, Manuel Chakravarty, |
|---|
| 208 | Sigbjorn Finne and Sven Panne, all of whom have made my life learning |
|---|
| 209 | Haskell all the more enjoyable by always being supportive. There were |
|---|
| 210 | doubtless others who helped and are not listed, but these are those who |
|---|
| 211 | come to mind. |
|---|
| 212 | |
|---|
| 213 | \- Hal Daumé III |
|---|
| 214 | |
|---|
| 215 | = Chapter 1 -- Introduction |
|---|
| 216 | |
|---|
| 217 | This tutorial contains a whole host of example code, all of which should |
|---|
| 218 | have been included in its distribution. If not, please refer to the |
|---|
| 219 | links off of the Haskell web site (`haskell.org`) to get it. This book |
|---|
| 220 | is formatted to make example code stand out from the rest of the text. |
|---|
| 221 | |
|---|
| 222 | Code will look like this. |
|---|
| 223 | |
|---|
| 224 | Occasionally, we will refer to interaction betwen you and the operating |
|---|
| 225 | system and/or the interactive shell (more on this in Section 2). |
|---|
| 226 | |
|---|
| 227 | Interaction will look like this. |
|---|
| 228 | |
|---|
| 229 | Strewn throughout the tutorial, we will often make additional notes to |
|---|
| 230 | something written. These are often for making comparisons to other |
|---|
| 231 | programming languages or adding helpful information. |
|---|
| 232 | |
|---|
| 233 | *NOTE* Notes will appear like this. |
|---|
| 234 | |
|---|
| 235 | If we're covering a difficult or confusing topic and there is something |
|---|
| 236 | you should watch out for, we will place a warning. |
|---|
| 237 | |
|---|
| 238 | *WARNING* Warnings will appear like this. |
|---|
| 239 | |
|---|
| 240 | Finally, we will sometimes make reference to built-in functions (so- |
|---|
| 241 | called Preludefunctions). This will look something like this: |
|---|
| 242 | |
|---|
| 243 | .math |
|---|
| 244 | map :: (a->b)->[a]->[b] |
|---|
| 245 | .math |
|---|
| 246 | |
|---|
| 247 | Within the body text, Haskell keywords will appear like this: *where*, |
|---|
| 248 | identifiers as `map`, types as /*String*/ and classes as */Eq/*. |
|---|
| 249 | |
|---|
| 250 | = Chapter 2 -- Getting Started |
|---|
| 251 | |
|---|
| 252 | There are three well known Haskell system: Hugs, GHC and NHC. Hugs is |
|---|
| 253 | exclusively an interpreter, meaning that you cannot compile stand-alone |
|---|
| 254 | programs with it, but can test and debug programs in an interactive |
|---|
| 255 | environment. GHC is both an interpreter (like Hugs) and a compiler which |
|---|
| 256 | will produce stand-alone programs. NHC is exclusively a compiler. Which |
|---|
| 257 | you use is entirely up to you. I've tried to make a list of some of the |
|---|
| 258 | differences in the following list but of course this is far from |
|---|
| 259 | exhaustive: |
|---|
| 260 | |
|---|
| 261 | - Hugs |
|---|
| 262 | very fast; implements almost all of Haskell 98 (the standard) and most |
|---|
| 263 | extensions; built-in support for module browsing; cannot create stand- |
|---|
| 264 | alones; written in C; works on almost every platform; build in graphics |
|---|
| 265 | library. |
|---|
| 266 | - GHC |
|---|
| 267 | interactive environment is slower than Hugs, but allows |
|---|
| 268 | function definitions in the environment (in Hugs you have to put them in |
|---|
| 269 | a file); implements all of Haskell 98 and extensions; good support for |
|---|
| 270 | interfacing with other languages; in a sense the "de facto" |
|---|
| 271 | standard. |
|---|
| 272 | - NHC |
|---|
| 273 | less used and no interactive environment, but produces smaller and often |
|---|
| 274 | faster executables than does GHC; supports Haskell 98 and some |
|---|
| 275 | extensions. |
|---|
| 276 | |
|---|
| 277 | I, personally, have all of them installed and use them for different |
|---|
| 278 | purposes. I tend to use GHC to compile (primarily because I'm most |
|---|
| 279 | familiar with it) and the Hugs interactive environment, since it is much |
|---|
| 280 | faster. As such, this is what I would suggest. However, that is a fair |
|---|
| 281 | amount to download an install, so if you had to go with just one, I'd |
|---|
| 282 | get GHC, since it contains both a compiler and interactive environment. |
|---|
| 283 | |
|---|
| 284 | Following is a descrition of how to download and install each of this as |
|---|
| 285 | of the time this tutorial was written. It may have changed -- see |
|---|
| 286 | http://haskell.org (the Haskell website) for up-to-date information. |
|---|
| 287 | |
|---|
| 288 | == 2.1 Hugs |
|---|
| 289 | |
|---|
| 290 | Hugs supports almost all of the Haskell 98 standard (it lacks some |
|---|
| 291 | of the libraries), as well as a number of advanced/experimental |
|---|
| 292 | extensions, including: multi-parameter type classes, extensible records, |
|---|
| 293 | rank-2 polymorphism, existentials, scoped type variables, and restricted |
|---|
| 294 | type synonyms. |
|---|
| 295 | |
|---|
| 296 | === 2.1.1 Where to get it |
|---|
| 297 | |
|---|
| 298 | The official Hugs web page is at: http://haskell.org/hugs. |
|---|
| 299 | |
|---|
| 300 | If you go there, there is a link titled "downloading" which will |
|---|
| 301 | send you to the download page. From that page, you can download the |
|---|
| 302 | appropriate version of Hugs for your computer. |
|---|
| 303 | |
|---|
| 304 | === 2.1.2 Installation procedures |
|---|
| 305 | |
|---|
| 306 | Once you've downloaded Hugs, installation differs depending on your |
|---|
| 307 | platform, however, installation for Hugs is more of less identical to |
|---|
| 308 | installation for any program on your platform. |
|---|
| 309 | |
|---|
| 310 | - For Windows |
|---|
| 311 | when you click on the "msi" file to download, simply choose "Run This |
|---|
| 312 | Program" and the installation will begin automatically. From there, just |
|---|
| 313 | follow the on-screen instructions. |
|---|
| 314 | - For RPMs |
|---|
| 315 | use whatever RPM installation program you know best. |
|---|
| 316 | - For source |
|---|
| 317 | first gunzip the file, then untar it. Presumably if you're using a |
|---|
| 318 | system which isn't otherwise supported, you know enough about your |
|---|
| 319 | system to be able to run configure scripts and make things by hand. |
|---|
| 320 | |
|---|
| 321 | == 2.1.3 How to run it |
|---|
| 322 | |
|---|
| 323 | On Unix machines, the Hugs interpreter is usually started with a command |
|---|
| 324 | line of the form: hugs `[option - file]` ... |
|---|
| 325 | |
|---|
| 326 | On Windows , Hugs may be started by selecting it from the start menu or |
|---|
| 327 | by double clicking on a file with the .hs or .lhs extension. (This |
|---|
| 328 | manual assumes that Hugs has already been successfully installed on your |
|---|
| 329 | system.) |
|---|
| 330 | |
|---|
| 331 | Hugs uses options to set system parameters. These options are |
|---|
| 332 | distinguished by a leading + or - and are used to customize the |
|---|
| 333 | behaviour of the interpreter. When Hugs starts, the interpreter performs |
|---|
| 334 | the following tasks: |
|---|
| 335 | |
|---|
| 336 | * Options in the environment are processed. The variable HUGSFLAGS holds |
|---|
| 337 | these options. On Windows 95/NT, the registry is also queried for Hugs |
|---|
| 338 | option settings. |
|---|
| 339 | * Command line options are processed. |
|---|
| 340 | * Internal data structures are initialized. In particular, the heap is |
|---|
| 341 | initialized, and its size is fixed at this point; if you want to run |
|---|
| 342 | the interpreter with a heap size other than the default, then this |
|---|
| 343 | must be specified using options on the command line, in the |
|---|
| 344 | environment or in the registry. |
|---|
| 345 | * The Prelude file is loaded. The interpreter will look for the Prelude |
|---|
| 346 | file on the path specified by the -P option. If the Prelude, located |
|---|
| 347 | in the file Prelude.hs, cannot be found in one of the path |
|---|
| 348 | directories or in the current directory, then Hugs will terminate; |
|---|
| 349 | Hugs will not run without the Prelude file. |
|---|
| 350 | * Program files specified on the command line are loaded. The effect of |
|---|
| 351 | a command hugs `f1 ... fn` is the same as starting up Hugs with the |
|---|
| 352 | hugs command and then typing `:load f1 ... fn`. In particular, the |
|---|
| 353 | interpreter will not terminate if a problem occurs while it is trying |
|---|
| 354 | to load one of the specified files, but it will abort the attempted |
|---|
| 355 | load command. The environment variables and command line options used |
|---|
| 356 | by Hugs are described in the following sections. |
|---|
| 357 | |
|---|
| 358 | === 2.1.4 Program options |
|---|
| 359 | |
|---|
| 360 | To list all of the options would take too much space. The most important |
|---|
| 361 | option at this point is "+98" or "-98". When you start hugs with "+98" |
|---|
| 362 | it is in Haskell 98 mode, which turns off all extensions. When you start |
|---|
| 363 | in "-98", you are in Hugs mode and all extensions are turned on. If |
|---|
| 364 | you've downloaded someone elses code and you're having trouble loading |
|---|
| 365 | it, first make sure you have the "98" flag set properly. |
|---|
| 366 | |
|---|
| 367 | Further information on the Hugs options is in the manual: |
|---|
| 368 | http://cvs.haskell.org/Hugs/pages/hugsman/started.html. |
|---|
| 369 | |
|---|
| 370 | === 2.1.5 How to get help |
|---|
| 371 | |
|---|
| 372 | To get Hugs specific help, go to the Hugs web page. To get general |
|---|
| 373 | Haskell help, go to the Haskell web page. |
|---|
| 374 | |
|---|
| 375 | == 2.2 Glasgow Haskell Compiler |
|---|
| 376 | |
|---|
| 377 | The Glasgow Haskell Compiler (GHC) is a robust, fully-featured, |
|---|
| 378 | optimising compiler and interactive environment for Haskell 98; GHC |
|---|
| 379 | compiles Haskell to either native code or C. It implements numerous |
|---|
| 380 | experimental language extensions to Haskell 98; for example: |
|---|
| 381 | concurrency, a foreign language interface, multi-parameter type classes, |
|---|
| 382 | scoped type variables, existential and universal quantification, unboxed |
|---|
| 383 | types, exceptions, weak pointers, and so on. GHC comes with a |
|---|
| 384 | generational garbage collector, and a space and time profiler. |
|---|
| 385 | |
|---|
| 386 | === 2.2.1 Where to get it |
|---|
| 387 | |
|---|
| 388 | Go to the official GHC web page http://haskell.org/ghc (GHC) to download |
|---|
| 389 | the latest release. The current version as of the writing of this |
|---|
| 390 | tutorial is 5.04.2 and can be downloaded off of the GHC download page |
|---|
| 391 | (follow the "Download" link). From that page, you can download the |
|---|
| 392 | appropriate version of GHC for your computer. |
|---|
| 393 | |
|---|
| 394 | === 2.2.2 Installation procedures |
|---|
| 395 | |
|---|
| 396 | Once you've downloaded GHC, installation differs depending on your |
|---|
| 397 | platform; however, installation for GHC is more of less identical to |
|---|
| 398 | installation for any program on your platform. |
|---|
| 399 | |
|---|
| 400 | - For Windows |
|---|
| 401 | when you click on the "msi" file to download, simply choose "Run This |
|---|
| 402 | Program" and the installation will begin automatically. From there, |
|---|
| 403 | just follow the on-screen instructions. |
|---|
| 404 | |
|---|
| 405 | - For RPMs |
|---|
| 406 | use whatever RPM installation program you know best. |
|---|
| 407 | |
|---|
| 408 | - For source |
|---|
| 409 | first gunzip the file, then untar it. Presumably if you're using a |
|---|
| 410 | system which isn't otherwise supported, you know enough about your |
|---|
| 411 | system to be able to run configure scripts and make things by hand. |
|---|
| 412 | |
|---|
| 413 | For a more detailed description of the installation procedure, look at |
|---|
| 414 | the GHC users manual under "Installing GHC". |
|---|
| 415 | |
|---|
| 416 | === 2.2.3 How to run the compiler |
|---|
| 417 | |
|---|
| 418 | Running the compiler is fairly easy. Assuming that you have a program |
|---|
| 419 | written with a mainfunction in a file called Main.hs, you can compile it |
|---|
| 420 | simply by writing: |
|---|
| 421 | |
|---|
| 422 | % ghc --make Main.hs -o main |
|---|
| 423 | |
|---|
| 424 | The "make" option tells GHC that this is a program and not just a |
|---|
| 425 | library and you want to build it and all modules it depends on. |
|---|
| 426 | "Main.hs" stipulates the name of the file to compile; and the "-o main" |
|---|
| 427 | means that you want to put the output in a file called "main". |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | *NOTE* In Windows, you should say "-o main.exe" to tell Windows |
|---|
| 431 | that this is an executable file. |
|---|
| 432 | |
|---|
| 433 | You can then run the program by simply typing "main" at the prompt. |
|---|
| 434 | |
|---|
| 435 | === 2.2.4 How to run the interpreter |
|---|
| 436 | |
|---|
| 437 | GHCi is invoked with the command "ghci" or "ghc -interactive". |
|---|
| 438 | One or more modules or filenames can also be specified on the command |
|---|
| 439 | line; this instructs GHCi to load the specified modules or filenames |
|---|
| 440 | (and all the modules they depend on), just as if you had said :load |
|---|
| 441 | modules at the GHCi prompt. |
|---|
| 442 | |
|---|
| 443 | === 2.2.5 Program options |
|---|
| 444 | |
|---|
| 445 | To list all of the options would take too much space. The most important |
|---|
| 446 | option at this point is "-fglasgow-exts". When you start GHCi without |
|---|
| 447 | "-fglasgow-exts" it is in Haskell 98 mode, which turns off all |
|---|
| 448 | extensions. When you start with "-fglasgowexts", all extensions are |
|---|
| 449 | turned on. If you've downloaded someone elses code and you're having |
|---|
| 450 | trouble loading it, first make sure you have this flag set properly. |
|---|
| 451 | |
|---|
| 452 | Further information on the GHC and GHCi options are in the manual off of the |
|---|
| 453 | GHC web page. |
|---|
| 454 | |
|---|
| 455 | === 2.2.6 How to get help |
|---|
| 456 | |
|---|
| 457 | To get GHC(i) specific help, go to the GHC web page. To get general |
|---|
| 458 | Haskell help, go to the Haskell web page. |
|---|
| 459 | |
|---|
| 460 | == 2.3 NHC |
|---|
| 461 | |
|---|
| 462 | About NHC 3 ... |
|---|
| 463 | |
|---|
| 464 | === 2.3.1 Where to get it |
|---|
| 465 | === 2.3.2 Installation procedures |
|---|
| 466 | === 2.3.3 How to run it |
|---|
| 467 | === 2.3.4 Program options |
|---|
| 468 | === 2.3.5 How to get help |
|---|
| 469 | |
|---|
| 470 | == 2.4 Editors |
|---|
| 471 | |
|---|
| 472 | With good text editor, programming is fun. Of course, you can get along |
|---|
| 473 | with simplistic editor capable of just cut-n-paste, but good editor is |
|---|
| 474 | capable of doing most of the chores for you, letting you concentrate on |
|---|
| 475 | what you are writing. With respect to programming in Haskell, good text |
|---|
| 476 | editor should have as much as possible of the following features: |
|---|
| 477 | |
|---|
| 478 | * Syntax highlighting for source files |
|---|
| 479 | * Indentation of source files |
|---|
| 480 | * Interaction with Haskell interpreter (be it Hugs or GHCi) |
|---|
| 481 | * Computer-aided code navigation |
|---|
| 482 | * Code completion |
|---|
| 483 | |
|---|
| 484 | At the time of writing, several options were available: Emacs/XEmacs |
|---|
| 485 | support Haskell via haskell-mode and accompanying Elist code (available |
|---|
| 486 | from http://www.haskell.org/haskellmode), and 3 ... . |
|---|
| 487 | |
|---|
| 488 | What's else available? ... |
|---|
| 489 | |
|---|
| 490 | (X)Emacs seem to do the best job, having all the features listed above. |
|---|
| 491 | Indentation is aware about Haskell's 2-dimensional layout rules (see |
|---|
| 492 | Section 7.11, very smart and have to be seen in action to be believed. |
|---|
| 493 | You can quickly jump to the definition of chosen function with the help |
|---|
| 494 | of "Definitions" menu, and name of the currently edited function is |
|---|
| 495 | always displayed in the modeline. |
|---|
| 496 | |
|---|
| 497 | = Chapter 3 -- Language Basics |
|---|
| 498 | |
|---|
| 499 | In this chapter we present the basic concepts of Haskell. In addition |
|---|
| 500 | to familiarizing you with the interactive environments and showing you |
|---|
| 501 | how to compile a basic program, we introduce the basic syntax of |
|---|
| 502 | Haskell, which will probably be quite alien if you are used to |
|---|
| 503 | languages like C and Java. |
|---|
| 504 | |
|---|
| 505 | However, before we talk about specifics of the language, we need to |
|---|
| 506 | establish some general properties of Haskell. Most importantly, |
|---|
| 507 | Haskell is a /lazy/ language, which lazy means that no computation |
|---|
| 508 | takes place unless it is forced to take place when the result of that |
|---|
| 509 | computation is used. |
|---|
| 510 | |
|---|
| 511 | This means, for instance, that you can define infinitely large data |
|---|
| 512 | structures, provided that you never use the entire structure. For |
|---|
| 513 | instance, using imperative-esque psuedo-code, we could create an |
|---|
| 514 | infinite list containing the number 4in each position by doing |
|---|
| 515 | something like: |
|---|
| 516 | |
|---|
| 517 | List makeList() |
|---|
| 518 | { |
|---|
| 519 | List current = new List(); |
|---|
| 520 | current.value = 1; |
|---|
| 521 | current.next = makeList(); |
|---|
| 522 | return current; |
|---|
| 523 | } |
|---|
| 524 | |
|---|
| 525 | By looking at this code, we can see what it's trying to do: it creates a |
|---|
| 526 | new list, sets its value to 4and then recursively calls itself to make |
|---|
| 527 | the rest of the list. Of course, if you actually wrote this code and |
|---|
| 528 | called it, the program would never terminate, because makeListwould keep |
|---|
| 529 | calling itself ad infinitum. |
|---|
| 530 | |
|---|
| 531 | This is because we assume this imperative-esque language is /strict/, |
|---|
| 532 | the opposite of strict lazy. Strict languages are often referred to as |
|---|
| 533 | "call by value," while lazy languages are referred to as "call by name." |
|---|
| 534 | In the above psuedo-code, when we "run" makeList on the fifth line, we |
|---|
| 535 | attempt to get a /value/ out of it. This leads to an infinite loop. |
|---|
| 536 | |
|---|
| 537 | The equivalent code in Haskell is: |
|---|
| 538 | |
|---|
| 539 | makeList = 1 : makeList |
|---|
| 540 | |
|---|
| 541 | This program reads: we're defining something called makeList(this is |
|---|
| 542 | what goes on the left-hand side of the equals sign). On the right-hand |
|---|
| 543 | side, we give the definition of makeList. In Haskell, the colon operator |
|---|
| 544 | is used to create lists (we'll talk more about this soon). This right- |
|---|
| 545 | hand side says that the value of makeListis the element 1stuck on to the |
|---|
| 546 | beginning of the value of makeList. |
|---|
| 547 | |
|---|
| 548 | However, since Haskell is lazy (or "call by name"), we do not actually |
|---|
| 549 | attempt to evaluate what makeListis at this point: we simply remember |
|---|
| 550 | that if ever in the future we need the second element of makeList, we |
|---|
| 551 | need to just look at makeList. |
|---|
| 552 | |
|---|
| 553 | Now, if you attempt to write makeListto a file, print it to the screen, |
|---|
| 554 | or calculate the sum of its elements, the operation won't terminate |
|---|
| 555 | because it would have to evaluate an infinitely long list. However, if |
|---|
| 556 | you simply use a finite portion of the list (say the first 45 elements), |
|---|
| 557 | the fact that the list is infinitely long doesn't matter. If you only |
|---|
| 558 | use the first 4/5elements, only the first 4/5elements are ever |
|---|
| 559 | calculated. This is laziness. |
|---|
| 560 | |
|---|
| 561 | Second, Haskell is case-sensitive. Many languages are, but Haskell |
|---|
| 562 | actually uses case to give meaning. Haskell distinguishes between /values/ |
|---|
| 563 | (for instance, numbers: 1, 2, 3, ...); strings: "abc", "hello", |
|---|
| 564 | ... ; characters: 'a', 'b', ' ', ... ; even functions: for instance, the |
|---|
| 565 | function squares a value, or the square-root function); and /types/ (the |
|---|
| 566 | categories to which values belong). |
|---|
| 567 | |
|---|
| 568 | By itself, this is not unusual. Most languages have some system of |
|---|
| 569 | types. What is unusual is that Haskell /requires/ that the names given to |
|---|
| 570 | functions and values begin with a lower- case letter and that the names |
|---|
| 571 | given to types begin with an upper-case letter. The moral is: if your |
|---|
| 572 | otherwise correct program won't compile, be sure you haven't named your |
|---|
| 573 | function Foo, or something else beginning with a capital letter. |
|---|
| 574 | |
|---|
| 575 | Being a functional language, Haskell eschews side effects. A side effect |
|---|
| 576 | is essentially something that happens in the course of executing a |
|---|
| 577 | function that is not related to the output produced by that function. |
|---|
| 578 | |
|---|
| 579 | For instance, in a language like C or Java, you are able to modify |
|---|
| 580 | "global" variables from within a function. This is a side effect because |
|---|
| 581 | the modification of this global variable is not related to the output |
|---|
| 582 | produced by the function. Furthermore, modifying the state of the real |
|---|
| 583 | world is considered a side effect: printing something to the screen, |
|---|
| 584 | reading a file, etc., are all side effecting operations. |
|---|
| 585 | |
|---|
| 586 | Functions that do not have side effects are called /pure/. An easy test |
|---|
| 587 | for whether or not a function is pure is to ask yourself a simple |
|---|
| 588 | question: "Given the same arguments, will this function always produce |
|---|
| 589 | the same result?". |
|---|
| 590 | |
|---|
| 591 | All of this means that if you're used to writing code in an imperative |
|---|
| 592 | language (like C or Java), you're going to have to start thinking |
|---|
| 593 | differently. Most importantly, if you have a value x, you must /not/ |
|---|
| 594 | think of x as a register, a memory location or anything else of that |
|---|
| 595 | nature. xis simply a name, just as "Hal" is my name. You cannot |
|---|
| 596 | arbitrarily decide to store a different person in my name any more than |
|---|
| 597 | you can arbitrarily decide to store a different value in x. This means |
|---|
| 598 | that code that might look like the following C code is invalid (and has |
|---|
| 599 | no counterpart) in Haskell: |
|---|
| 600 | |
|---|
| 601 | int x = 5; |
|---|
| 602 | x = x + 1; |
|---|
| 603 | |
|---|
| 604 | A call like x = x + 1 is called /destructive update/ because we are |
|---|
| 605 | destroying whatever was in x before and replacing it with a new value. |
|---|
| 606 | Destructive update does not exist in Haskell. |
|---|
| 607 | |
|---|
| 608 | By not allowing destructive updates (or any other such side effecting |
|---|
| 609 | operations), Haskell code is very easy to comprehend. That is, when we |
|---|
| 610 | define a function `f`, and call that function with a particular argument |
|---|
| 611 | `a` in the beginning of a program, and then, at the end of the program, |
|---|
| 612 | again call `f` with the same argument `a`, we know we will get out the |
|---|
| 613 | same result. This is because we know that `a` cannot have changed and |
|---|
| 614 | because we know that `f` only depends on `a` (for instance, it didn't |
|---|
| 615 | increment a global counter). This property is called /referential |
|---|
| 616 | transparency/ and basically states that if two functions `f` and `g` |
|---|
| 617 | produce the same values for the same arguments, then we may replace `f` |
|---|
| 618 | with `g` (and vice-versa). |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | *NOTE* There is no agreed-upon exact definition of referential |
|---|
| 622 | transparency. The definition given above is the one I like best. |
|---|
| 623 | They all carry the same interpretation; the differences lie in how |
|---|
| 624 | they are formalized. |
|---|
| 625 | |
|---|
| 626 | == 3.1 Arithmetic |
|---|
| 627 | |
|---|
| 628 | Let's begin our foray into Haskell with simple arithmetic. Start up your |
|---|
| 629 | favorite interactive shell (Hugs or GHCi; see Chapter 2 for installation |
|---|
| 630 | instructions). The shell will output to the screen a few lines talking |
|---|
| 631 | about itself and what it's doing and then should finish with the cursor |
|---|
| 632 | on a line reading: |
|---|
| 633 | |
|---|
| 634 | Prelude> |
|---|
| 635 | |
|---|
| 636 | From here, you can begin to evaluate /expressions/. An expression is |
|---|
| 637 | basically something that has a value. For instance, the number `5` is an |
|---|
| 638 | expression (its value is 5). Val ues can be built up from other values; |
|---|
| 639 | for instance, `5 + 6` is an expression (its value is 11). In fact, most |
|---|
| 640 | simple arithmetic operations are supported by Haskell, including plus |
|---|
| 641 | (+), minus (-), times (*), divided-by (/), exponentiation (^) and square- |
|---|
| 642 | root (sqrt). You can experiment with these by asking the interactive |
|---|
| 643 | shell to evaluate expressions and to give you their value. In this way, |
|---|
| 644 | a Haskell shell can be used as a powerful calculator. Try some of the |
|---|
| 645 | following: |
|---|
| 646 | |
|---|
| 647 | Prelude> 5*4+3 |
|---|
| 648 | 23 |
|---|
| 649 | Prelude> 5^5-2 |
|---|
| 650 | 3123 |
|---|
| 651 | Prelude> sqrt 2 |
|---|
| 652 | 1.4142135623730951 |
|---|
| 653 | Prelude> 5*(4+3) |
|---|
| 654 | 35 |
|---|
| 655 | |
|---|
| 656 | We can see that, in addition to the standard arithmetic operations, |
|---|
| 657 | Haskell also allows grouping by parentheses, hence the difference |
|---|
| 658 | between the values of *5*4+3* and *5*(4+3)*. The reason for this is that |
|---|
| 659 | the "understood" grouping of the first expression is operator precedence |
|---|
| 660 | *(5*4)+3*, due to /operator precedence/. |
|---|
| 661 | |
|---|
| 662 | Also note that parentheses aren't required around function arguments. |
|---|
| 663 | For instance, we simply wrote `sqrt 2`, not `sqrt(2)`, as would be |
|---|
| 664 | required in most other languages. You could write it with the |
|---|
| 665 | parentheses, but in Haskell, since function application is so common, |
|---|
| 666 | parentheses aren't required. |
|---|
| 667 | |
|---|
| 668 | .warning |
|---|
| 669 | Even though parentheses are not always needed, sometimes it is better to |
|---|
| 670 | leave them in anyway; other people will probably have to read your code, |
|---|
| 671 | and if extra parentheses make the intent of the code clearer, use them. |
|---|
| 672 | .warning. |
|---|
| 673 | |
|---|
| 674 | Now try entering *2.5000*. Does it work? |
|---|
| 675 | |
|---|
| 676 | .note |
|---|
| 677 | If you're familiar with programming in other languages, you may find |
|---|
| 678 | it odd that *sqrt 2* comes back with a decimal point (i.e., is a |
|---|
| 679 | floating point number) even though the argument to the function seems |
|---|
| 680 | to be an integer. This interchangability of numeric types is due to |
|---|
| 681 | Haskell's system of /type classes/ and will be discussed in detail in |
|---|
| 682 | Section 4.3). |
|---|
| 683 | .note. |
|---|
| 684 | |
|---|
| 685 | .exercises |
|---|
| 686 | - Exercise 3.1 |
|---|
| 687 | We've seen that multiplication binds more tightly than division. Can you |
|---|
| 688 | think of a way to determine whether function application binds more or |
|---|
| 689 | less tightly than multiplication? |
|---|
| 690 | .exercises. |
|---|
| 691 | |
|---|
| 692 | == 3.2 Pairs, Triples and More |
|---|
| 693 | |
|---|
| 694 | In addition to single values, we should also address multiple values. |
|---|
| 695 | For instance, we may want to refer to a position by its @/Acoordinate, |
|---|
| 696 | which would be a pair of integers. To make a pair of integers is simple: |
|---|
| 697 | you enclose the pair in parenthesis and separate them with a comma. Try |
|---|
| 698 | the following: |
|---|
| 699 | |
|---|
| 700 | Prelude> (5,3) |
|---|
| 701 | (5,3) |
|---|