root/docs/yaht.kwid

Revision 5592, 31.3 kB (checked in by iblech, 3 years ago)

Usual svn props, EOLs at EOFs, minor doc and Haddock fixes.

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