Programming in Haskell Second Edition, 5Th Printing Edition Hutton All Chapter Instant Download
Programming in Haskell Second Edition, 5Th Printing Edition Hutton All Chapter Instant Download
com
https://textbookfull.com/product/programming-in-haskell-
second-edition-5th-printing-edition-hutton/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/programming-in-haskell-2nd-edition-
graham-hutton/
textboxfull.com
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-loucas/
textboxfull.com
https://textbookfull.com/product/thinking-with-types-type-level-
programming-in-haskell-sandy-maguire/
textboxfull.com
https://textbookfull.com/product/haskell-the-ultimate-beginner-s-
guide-to-learn-haskell-programming-step-by-step-1st-edition-claudia-
alves/
textboxfull.com
The Haskell Road to Logic Maths and Programming Kees Doets
https://textbookfull.com/product/the-haskell-road-to-logic-maths-and-
programming-kees-doets/
textboxfull.com
https://textbookfull.com/product/the-information-bomb-radical-
thinkers-second-printing-edition-virilio-paul/
textboxfull.com
https://textbookfull.com/product/programming-microsoft-dynamics-
nav-5th-edition-mark-brummel/
textboxfull.com
https://textbookfull.com/product/postgresql-server-programming-second-
edition-dar/
textboxfull.com
https://textbookfull.com/product/copyrights-highway-from-the-printing-
press-to-the-cloud-second-edition-paul-goldstein/
textboxfull.com
Programming in Haskell
Second Edition
Haskell is a purely functional language that allows programmers to
rapidly develop clear, concise and correct software. The language
has grown in popularity in recent years, both in teaching and in
industry. This book is based on the author’s experience of teaching
Haskell for more than 20 years. All concepts are explained from first
principles and no programming experience is required, making this
book accessible to a broad spectrum of readers. While Part I focuses
on basic concepts, Part II introduces the reader to more advanced
topics.
This new edition has been extensively updated and expanded to
include recent and more advanced features of Haskell, new
examples and exercises, selected solutions, and freely downloadable
lecture slides and code. The presentation is clean and simple, while
also being fully compliant with the latest version of the language,
including recent changes concerning applicative, monadic, foldable
and traversable types.
GRAHAM HUTTON is Professor of Computer Science at the University of
Nottingham. He has taught Haskell to thousands of students and
received numerous best lecturer awards. Hutton has served as an
editor of the Journal of Functional Programming, chair of the Haskell
Symposium and the International Conference on Functional
Programming, vice-chair of the ACM Special Interest Group on
Programming Languages, and he is an ACM Distinguished Scientist.
Programming in Haskell
Second Edition
GRAHAM HUTTON
University of Nottingham
University Printing House, Cambridge CB2 8BS, United Kingdom
www.cambridge.org
Information on this title: www.cambridge.org/9781316626221
10.1017/9781316784099
A catalogue record for this publication is available from the British Library
Cambridge University Press has no responsibility for the persistence or accuracy of URLs for
external or third-party Internet Web sites referred to in this publication, and does not
guarantee that any content on such Web sites is, or will remain, accurate or appropriate.
For Annette, Callum and Tom
Contents
Foreword
Preface
2 First steps
2.1 Glasgow Haskell Compiler
2.2 Installing and starting
2.3 Standard prelude
2.4 Function application
2.5 Haskell scripts
2.6 Chapter remarks
2.7 Exercises
4 Defining functions
4.1 New from old
4.2 Conditional expressions
4.3 Guarded equations
4.4 Pattern matching
4.5 Lambda expressions
4.6 Operator sections
4.7 Chapter remarks
4.8 Exercises
5 List comprehensions
5.1 Basic concepts
5.2 Guards
5.3 The zip function
5.4 String comprehensions
5.5 The Caesar cipher
5.6 Chapter remarks
5.7 Exercises
6 Recursive functions
6.1 Basic concepts
6.2 Recursion on lists
6.3 Multiple arguments
6.4 Multiple recursion
6.5 Mutual recursion
6.6 Advice on recursion
6.7 Chapter remarks
6.8 Exercises
7 Higher-order functions
7.1 Basic concepts
7.2 Processing lists
7.3 The foldr function
7.4 The foldl function
7.5 The composition operator
7.6 Binary string transmitter
7.7 Voting algorithms
7.8 Chapter remarks
7.9 Exercises
11 Unbeatable tic-tac-toe
11.1 Introduction
11.2 Basic declarations
11.3 Grid utilities
11.4 Displaying a grid
11.5 Making a move
11.6 Reading a number
11.7 Human vs human
11.8 Game trees
11.9 Pruning the tree
11.10 Minimax algorithm
11.11 Human vs computer
11.12 Chapter remarks
11.13 Exercises
13 Monadic parsing
13.1 What is a parser?
13.2 Parsers as functions
13.3 Basic definitions
13.4 Sequencing parsers
13.5 Making choices
13.6 Derived primitives
13.7 Handling spacing
13.8 Arithmetic expressions
13.9 Calculator
13.10 Chapter remarks
13.11 Exercises
15 Lazy evaluation
15.1 Introduction
15.2 Evaluation strategies
15.3 Termination
15.4 Number of reductions
15.5 Infinite structures
15.6 Modular programming
15.7 Strict application
15.8 Chapter remarks
15.9 Exercises
17 Calculating compilers
17.1 Introduction
17.2 Syntax and semantics
17.3 Adding a stack
17.4 Adding a continuation
17.5 Defunctionalising
17.6 Combining the steps
17.7 Chapter remarks
17.8 Exercises
Bibliography
Index
Foreword
How is it structured?
The book is divided into two parts. Part I introduces the basic
concepts of pure programming in Haskell and is structured around
the core features of the language, such as types, functions, list
comprehensions, recursion and higher-order functions. Part II covers
impure programming and a range of more advanced topics, such as
monads, parsing, foldable types, lazy evaluation and reasoning
about programs. The book contains many extended programming
examples, and each chapter includes suggestions for further reading
and a series of exercises. The appendices provide solutions to
selected exercises, and a summary of some of the most commonly
used definitions from the Haskell standard prelude.
Acknowledgements
I am grateful to the University of Nottingham for providing a
sabbatical to produce this new edition; Thorsten Altenkirch, Venanzio
Capretta, Henrik Nilsson and other members of the FP lab for our
many enjoyable discussions; Iván Pérez Domínguez for useful
comments on a number of chapters; the students and tutors on all
of my Haskell courses for their feedback; Clare Dennison, David
Tranah and Abigail Walkington at CUP for their editorial work; the
GHC team for producing such a great compiler; and finally, Catherine
and Ian Hutton for getting me started in computing all those years
ago.
Many thanks also to Ki Yung Ahn, Bob Davison, Philip Hölzenspies
and Neil Mitchell for providing detailed comments on the first
edition, and to the following for pointing our errors and typos: Paul
Brown, Sergio Queiroz de Medeiros, David Duke, Robert Fabian, Ben
Fleis, Robert Furber, Andrew Kish, Tomoyas Kobayashi, Florian
Larysch, Carlos Oroz, Douglas Philips, Bruce Turner, Gregor Ulm,
Marco Valtorta and Kazu Yamamoto. All of these comments have
been taken into account when preparing the new edition.
Graham Hutton
Part I
Basic Concepts
1
Introduction
In this chapter we set the stage for the rest of the book. We start by
reviewing the notion of a function, then introduce the concept of
functional programming, summarise the main features of Haskell and
its historical background, and conclude with three small examples
that give a taste of Haskell.
1.1 Functions
In Haskell, a function is a mapping that takes one or more
arguments and produces a single result, and is defined using an
equation that gives a name for the function, a name for each of its
arguments, and a body that specifies how the result can be
calculated in terms of the arguments.
For example, a function double that takes a number x as its
argument, and produces the result x + x, can be defined by the
following equation:
double x = x + x
However, this approach requires two more steps than our original
version, because the expression double 2 is duplicated in the first
step and hence simplified twice. In general, the order in which
functions are applied in a calculation does not affect the value of the
final result, but it may affect the number of steps required, and
whether the calculation process terminates. These issues are
explored in more detail when we consider how expressions are
evaluated in chapter 15.
That is, we first initialise an integer variable total to zero, and then
enter a loop that ranges an integer variable count from 1 to n,
adding the current value of the counter to the total each time round
the loop.
In the above program, the basic method of computation is
changing stored values, in the sense that executing the program
results in a sequence of assignments. For example, the case of n = 5
gives the following sequence, in which the final value assigned to
the variable total is the required sum:
total = 0;
count = 1;
total = 1;
count = 2;
total = 3;
count = 3;
total = 6;
count = 4;
total = 10;
count = 5;
total = 15;
Twelfth Hypothesis.
(I must premise, that in all I have now to say, not only on the
present hypothesis, but on all those that follow, I build on this
principle; that, from the moment every individual pays the tax,
nobody pays it; that nevertheless its produce reverts to the creditors
of the State, and that therefore every body is interested in carrying
matters to that issue as soon as possible.—If there never existed in
the average of prices any revolution so sudden, so great, and
constant in its effect, as the one which I am about to suppose; it is
because it never was necessary to lay all at once 10 millions of
taxes. The effect produced by one million on a revenue like that
which England enjoys, is so trifling, so gradual, that it never can be
felt.—But to my hypothesis).
The march of industry is always firm; she can never be bewildered
by her guide. A tax of 2 shillings upon a hat will soon occasion either
an alteration in the quality, or a proportionate increase in its price[7].
Nothing more just. But agriculture has only one resource; she
cannot alter the quality of her productions, and she is always timid
when she wants to increase their prices. Let it be granted, however,
that the first attempt of the cultivator will be to enhance boldly the
price of his goods, in a proportion which may return him the amount
of the tax supposed to be of 10 millions: his wheat, of course, will
rise from 40 to 46s. 8d. and so with regard to other productions;
then his revenue, instead of 60, will be 70 millions; but, being
obliged to lay down 10 for the tax, he will have only 60 millions left.
Let us now observe, that when the operations of agriculture are
talked of in consequence of taxes, the land proprietor is alluded to;
and from him they suppose that the ten millions are taken, just as
the fleece is shorn from the sheep; but, on the other hand, that
proprietor, though not very clear in his idea of what is best for him
to do, feels mechanically that he has a right to defend his fleece,
that is, to increase the price of the productions of his land, because
without them, neither the manufacturer nor the Minister of Finance
can subsist, and to raise that price to such a level as will subject him
to pay only just as much as the minister and the manufacturer: and
this again appears to me extremely just.
Now this very proprietor receives of his 60 millions revenue, or
rather of the revenue of his land, no more than about 20 millions;
(this is nearly the result of Mr. Young’s statement: it will be seen, in
the sequel, that more or less would only alter the proportion of the
shares, without having the least influence either on the application
of the principle, or on its consequences): the 40 millions overplus is
swallowed up, viz. for the daily pay of the labourer, 20 millions, and
20 millions for the other charges with which the estates are
encumbered, tythes, profits of farmers, &c. &c. It follows, of course,
Either
That the proprietor’s share must be advanced to 30 millions, if he
alone be taxed; and this will leave each of the other parts interested
in the landed revenue, in possession of its 20 millions, as well after
as before the tax;
Or,
That each of the three parts taxed singly, at 3,333,333l. 6s. 8d.
and enabled to pay the impost by an advance of price, both in the
productions and in the wages of labour, be reduced to its primitive
20 millions, after the payment of the tax.
One is evidently equal to the other; but what is not so, is, that
industry, who paid her workmen with 20 millions when the quarter
of wheat was at 40s. cannot pay them since the wheat is increased
to 46s. 8d. but by adding to the former wages, 3,333,333l. 6s. 8d.
an advance which, when divided on the total of her products,
heretofore rated at 60 millions, raises each third part of 20 to
21,111,111, and some trifling fractions.—Now, there remains in the
hands of each of the parties concerned in agriculture, only 20
millions over and above the payment of the tax.—Those who are in
the persuasion that the discharge of the import must lessen, by its
whole amount at least, the revenue of those who pay it, think, no
doubt, that agriculture is very well off, to have, by raising the price
of its commodities two thirds, lightened the weight of so terrible a
burden; yet if it were possible by a vigorous exertion, by an
additional increase in the price of provisions, to make it null to every
body, I do not see that it would be reasonable to oppose such
exertion; for, after all,
Either
The first increase, brought on by agriculture, was unjust; the
proprietor was doomed to sustain alone the whole weight of the tax,
if we allow the taxator, the man who laid the tax, to have had a
single thought on the subject:
Or,
The further advance in the prices of agriculture is of the most
indispensable necessity, if the taxator has only thought of getting the
amount of the tax with the least trouble to himself, leaving to Nature
the care of distributing to every one complete justice,—to Nature,
who never fails to do it, and who, in so complicated an operation,
employs only the simple spring of that private cupidity, with which
she has armed and shielded every individual; just as, in order to
settle the most exact symmetry in that admirable edifice commonly
called a hive, she employs no other agent than the reciprocal
pressure of that multitude of architects who work at it, each of
whom thinks of nothing but to secure a little cell for himself.
Thirteenth Hypothesis.
Let us then suppose that agriculture, judiciously determined by
wisdom not to suffer the least encroachments on any part of her
cell, or mechanically led by cupidity to justice, boldly raises the price
of her wheat from 40 to 50s. and the rest of her products in a due
proportion; her general revenues will then be increased from 60 to
75 millions; each of the three parts interested therein will therefore
stand at 25 millions, but will be reduced to 21,666,666l. 13s. 4d.
when the tax is paid off;—
Industry, compelled also by the advanced prices of the
productions of the earth, to pay her workmen 25 instead of the 20
millions which they required in the first instance, will divide the
additional 5 millions amongst the 60 millions of her former returns;
each third share therefore, rated hitherto at 20 millions, will, by
means of the addition, rise to 21,666,666l. 13s. 4d.—which is the
exact balance of the like sum left in the hands of each of the three
branches of agriculture, after having discharged the tax.
In a system of taxation which should be imposed only on the land,
an impost of 10 millions annually, on a revenue of 60 millions,
whether it fall on the proprietor, or be divided between him, the
farmer, the cultivator, and other parties concerned, requires of
course an addition of 25 per cent. to the price of the productions of
the earth, that agriculture may not be sacrificed to industry; but it
must be observed, that immediately after the reaction of the one
upon the other shall be completed, the burden of the tax will
evidently be null, since the price of labour will have increased
equally in both, in proportion to that of their respective products.
Reflexions on the foregoing System.
This system was devised to countenance and encourage
agriculture; it is in her hands, they say, it is at that fountain-head of
riches, that riches must be sought for; it is the land that finally pays
for all; the impost then must be laid on the net produce of the land.
—I shall venture some reflexions on a subject which appears to me
the more important, as one of the wisest, and the most profound
speeches ever delivered in the House of Lords, seems to hint at the
expediency of an aggravation of the burden already laid on
agriculture.
Agriculture is the spring of life; it is not the spring of those riches
which we call money: money must be had for taxes; money flows
from the hands of industry alone: it is then from this spring that
money must be drawn.
Besides, how is it known that the money, wrested from
agriculture, is not necessary to her support? Or how does it escape
observation, that all the money which is not laid out by the cultivator
upon agriculture, will necessarily revert to industry, either by the
immediate consumption of the cultivator, or by that which he shall
have occasioned on the part of the person to whom he shall have
lent the value of what he has not consumed himself?
How comes it to be known, that the proprietor of a landed estate
that yields him, we shall suppose, 800l. per annum, and who is
charged 200l. for taxes, is not indebted in 300l. out of the remaining
600l.? Now, it is true that 300l. are sufficient for his support; but
were those 200l. which are forced from him, laid out annually on his
land, they would certainly in very few years raise its produce from
800 to 1000l.—What is that but to assess unjustly a man who enjoys
only 500l. as high as another whose income amounts to 800l.—to tie
down agriculture to 800 when it might rise to 1000; to deprive
industry of the re-action produced by that benefit; to crush
population in the very bud, and consequently to prevent a more
considerable consumption; in fine, to deprive government
beforehand of the amount of those taxes which that increase of
population and consumption would have secured to the State?
It may be said in France, it is true, that by a tax laid on private
loan-covenants, those inconveniences are partly removed; but this
very removal is only an additional obstacle, thrown in the way of
agriculture, to impede its progress;—the man who could lend to the
cultivator, besides the fear of not receiving back his money when
wanted, has before his eyes the certainty of being charged with the
twentieth penny laid on the interest of the money thus advanced,
and consequently, as often as he conveniently can, lends that money
to a merchant, who besides his punctuality in paying at the fixed
time, never hesitates to allow the usual interest without any
deduction whatever.
It may be said also in England, that all over Great Britain, the
land-tax, (which, like some others, is perhaps no more than a real
poll-tax under a name less grating to a British ear) does not exceed
the sum of about 2 millions out of 14 or 15, the whole amount of
her taxes; but nothing can be deduced therefrom, but that it is
subject only to a proportionable share of the inconveniences above
stated; inconveniences which are inseparable from it: we may add,
that it is indeed free from some further grievances which fall heavy
on the French cultivator:—but can the English boast of their tax
being founded on an invariable principle, when this very principle is
unjust in its consequences, and, as often as necessity requires the
tax to be advanced, multiplies the injustice which follows it?—Yet
they hint at the expediency of increasing that very tax, in order to
alleviate the imaginary burden that weighs down industry!—Some
pretend that the land-tax is become a public property; they suppose
that all such sales and purchases of land as have taken place since
the year 1740, have been effected on that principle of renunciation
of the capital, the interest of which is paid by that land-tax:—they
infer, no doubt, that the intention of those who did not sell, was
implicitly included in that of those who have actually sold; for they
propose that all the present proprietors shall redeem the tax, in
order to refund part of the national debt; as if, on the very next day
after the redemption, Government had not a right to renew it, if
some fresh exigency should start, which could not be answered by
any other means.
In the critical moment of a discredit, which will always be
imputable to the Minister, when the King and the People are fully
acquainted with their real interest, if administration, not knowing
what to do, does not hesitate to apply to the land proprietor, whose
ways and means are always visible, and whose purse lies, as it were,
ever open against his own inclination, the Minister, no doubt, finds
his excuse in the necessity; but after the crisis is over, why should
the proprietor be further aggrieved?
But, say they, the amount of the tax cannot be dispensed with;—
no objection can be urged against necessity; all that is wanted, is
the least that can possibly be given; just as all that is wanted, is the
most that it can be advantageous to take: but the whole should be
paid by each individual, only in proportion to his faculties:—Now the
fact is, that the faculties of the land proprietors are not better known
than those of the merchants;—the latter have found means to prove,
to persuade, that they ought not to be taxed, but in proportion to
their consumption, the only criterion of their riches, or at least that
part of their riches which they do not lay out in increasing
commercial wealth.—Is the improvement of the territorial wealth less
essential, or less valuable to the State?
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com