Programming in Haskell Second Edition, 5Th Printing Edition Hutton download pdf
Programming in Haskell Second Edition, 5Th Printing Edition Hutton download pdf
com
https://textbookfull.com/product/programming-in-
haskell-second-edition-5th-printing-edition-
hutton/
https://textbookfull.com/product/programming-in-haskell-2nd-edition-
graham-hutton/
textbookfull.com
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-loucas/
textbookfull.com
https://textbookfull.com/product/thinking-with-types-type-level-
programming-in-haskell-sandy-maguire/
textbookfull.com
https://textbookfull.com/product/theory-research-and-dynamics-of-
career-wellbeing-becoming-fit-for-the-future-ingrid-l-potgieter/
textbookfull.com
Towards a Philosophy of Caring in Higher Education
Pedagogy and Nuances of Care Yusef Waghid
https://textbookfull.com/product/towards-a-philosophy-of-caring-in-
higher-education-pedagogy-and-nuances-of-care-yusef-waghid/
textbookfull.com
https://textbookfull.com/product/a-case-based-approach-to-emergency-
psychiatry-1st-edition-maloy/
textbookfull.com
https://textbookfull.com/product/texas-politics-
today-2017-2018-edition-mark-p-jones/
textbookfull.com
https://textbookfull.com/product/application-of-peptide-based-prodrug-
chemistry-in-drug-development-1st-edition-arnab-de-auth/
textbookfull.com
https://textbookfull.com/product/the-brothers-corona-a-novel-the-
americas-series-1st-edition-guedea-rogelio/
textbookfull.com
Moral Engines Exploring The Ethical Drives In Human Life
Cheryl Mattingly
https://textbookfull.com/product/moral-engines-exploring-the-ethical-
drives-in-human-life-cheryl-mattingly/
textbookfull.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
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
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;
Language: English
By ELLIS HART
They dropped the few inches to the ground, bouncing a bit more
than they'd allowed for, in the lessened gravity of Diamore. All
around them the screams of the jungle meshed into one primal roar.
Shreve ran his tongue around the inside of his cheek. The medic had
flatly refused to allow their exit, unless they submitted to the six
shots he felt were minimum safety precaution.
With the feel of the electro-syringe still in his cheeks, Shreve
stepped away from the monstrous plug-port, raising his arms in
friendship. Behind him, Teller did the same.
They moved slowly toward the Diamoraii. The twelve sat immobile,
yet seeming to be looking from each other to the Earthmen, and
back, in sharp, jerking motions. It was all illusion, but disquieting.
As they stepped toward the aliens, Shreve felt the nerves in his teeth
begin to twitch. He had been about to say something soothing in
English, but the words never came out.
Who are you?
The question appeared in his head full-blown, inquisitive, without
sense of direction or distance. He knew immediately from where it
had come, of course, yet he could not quite believe it. Shreve
stopped dead, the pain in his jaws mounting. He glanced quickly at
Teller.
The shorter man was clutching his jaws with both hands, biting his
lower lip and rocking back and forth, eyes half-closed.
"Karl," Shreve's tongue stumbled over the words in his pain, "they're
—migod, Karl—they're telepathic!"
They stood rooted in their tracks, staring at the twelve impassive
aliens in their grotesque masks.
Teller stared in open fascination, still clutching his head. "The first,"
he murmured in awe. "The very first! They always said someday
we'd meet them, and now, by God, we have!" His voice died off to a
whisper and he stared unblinking at the dark-skinned Diamoraii.
The words appeared in their minds once more—this time more firm,
tinged with impatience:
Who are you?
Shreve seemed unable to respond. He had thought them ignorant
savages, on the verge of disaster, who would be jubilant at the offer
of aid. Instead, he was faced with making contact; contact with the
first mind-reading race Humanity had met racing through the stars.
His throat tightened up, he could not speak.
Finally, he took a step forward, extended his hands in peace to the
aliens. "Friends. We've come to help you. Friends."
He was certain they couldn't understand the spoken words. Whether
or not they could decipher the thoughts—that was something else.
Later, the Earthmen could bring out the communicators if the need
arose. But for now, he wanted only the soothing good will in his
voice to win them.
If they knew the Earthmen were protected by stat-fields, and that a
dozen gun-blisters were trained on them, they gave no indication.
We don't want your help.
There was a tone of anger, a driving odor of fear, in the feel of the
thoughts. The Earthmen felt their teeth jump as the thoughts
materialized. Shreve realized suddenly that the toothaches must be a
by-product of the psi power.
Shreve turned to Teller. The psych officer was staring back at him,
his eyes wide, his hands still clutching his jaw. They both recognized
something they had missed when the telepathy first became known
to them.
It was not an entering of the mind; they could not reach into the
deepest recesses of the Diamoraii's minds and get whole pictures. It
was like a mental radio transmission.
They could send and receive, with inflection and depth, but they had
to do it in darkness.
Teller said nothing, but he stepped closer to the aliens. Shreve could
tell he was thinking at them, but what he was thinking was
impossible to guess. If the aliens understood, they gave no
indication. The transmission did not work between the Earthmen,
obviously.
When Teller had fallen back, Shreve asked, "What did you say?"
"I told them we were here because volcanic eruptions were going to
rip up their planet within five months. I told them the quakes and
volcanos would kill off ninety-five percent of their people. I told them
we could help them to—"
The aliens rose slowly, and one stepped forward. He looked down at
the two Earthmen.
Hear this you are not the first strangers to come here once before
strange men came to us from the sky they called themselves the
Kyben and they told us they wanted to trade but they did not trade
they ate away our land and burned our jungle and took our women
and killed our young warriors.
It came as a blast of pure thought. All at once, as though spurted
out whole from the mind. The inflection was there—the meaning—
the depth of bitterness. Shreve felt his mouth dry out at the calibre
of agony in the thoughts.
Teller shrugged his shoulders as though he wanted no more part in
the matter, and retreated a few steps, massaging his throbbing jaws.
The alien stream ceased, and the Diamorai drew back. He seemed to
rise up on his toes, as though he wanted to strike the Earthman, but
was restraining himself through the movement.
The Wallower spun slowly in space, the eternal dust of the universe
lapping at her ports. Below her, enveloped by clouds of steam, the
planet Diamore blasted and erupted and screamed and belched and
tore itself apart.
Luther Shreve sat before the control console, staring with almost
hypnotized attention at the view-plates. He watched the world die.
His face was hard and unyielding. He had refused entrance even to
Teller, barring everyone from the control room.
At every eruption, with each fissure that opened wide enough to be
seen from that fantastic height, he felt a strange sinking in his heart.
His throat was dry, and there was an odd pressure behind his eyes.
He watched silently, every once in a while letting the thought They
didn't know when to ask for help filter through his mind.
The Group of Deciders huddled in the blasted Council Hall. The floor
—what was left of the inlaid tiles—shivered and heaved. Beyond the
twisted lattices of the windows they could hear the mighty rending
of the planet as it opened and swallowed all that stood.
Within an hour of the first eruptions, so quickly and with such fury
that there had been no time for preparation, almost three-fifths of
their race had been decimated.
The cities Kes and Uykvabask and Laylor had gone under with
roaring flames and the scraping of stone against flesh. The Great
Ocean had exploded with a red-hot bubbling and roared onto the
land, washing everything before it. The lava flows raced Eastward to
the Ceremonial Grounds and Westward to the Hunting Preserve.
Everywhere the ground opened without warning or reason, and life
sank beneath the earth.
Wrong, the Group of Deciders admitted in their last refuge. We were
wrong we have been foolish we have rejected our only salvation we
must prepare the group-mind send our plea for aid into space speak
to the outsiders ask them to help us.
They thought their instructions away from themselves, to their kin
across Diamore's blasted face. Prepare! Join! Speak to the outsiders!
And when they had gathered together every last Diamorai, with
more dying as they joined the chain, with the feel of agony radiating
through the group-mind, the message weakly rose. Tentatively it
probed at the inner surface of Diamore's atmosphere.
The power was, perhaps, insufficient to reach the spaceship. Three-
fifths of the Diamoraii were lost to the group-mind.
The group-mind struggled, frantically beaming, in hopelessness
trying to get through to the Earthmen who rode above them.
The men who rode above them—waiting for a signal from the
Diamoraii.
Shreve turned away from the plates, flicking them off. "I can't stand
it, Karl! How senseless! Because one race dealt them unfairly, they
closed their eyes to help from anyone else."
Teller crossed his legs as he sat on the couch. He did not appear to
be disturbed by the sight from below.
"Luther, you can't go on destroying yourself. You did everything you
could. You were as resourceful as any man could have been.
"Now you'd better get back to the schedule. We're over four and a
half months due at our next landfall." He saw his words were having
no effect. "Look, Luther, I've been in this business almost as long as
you. I've seen this time and again. When you come up against an
adolescent race, that doesn't know when it's got something too big
to handle, there's nothing you can do but back off and let them
handle it themselves. If they don't get smart enough to know when
to call the fireman—that's their agony. Not yours!"
"What's the next stop on our itinerary?" he asked the last almost
jauntily, consciously trying to take Shreve's mind off the cinder that
spun below the Wallower. He rose and stretched, as though from a
profound sleep.
For a moment he stared in wonder. Then he stepped into the shaft
and quietly left the control room.
He had never thought he'd see the day when Luther Shreve cried
like a child.
THE END
*** END OF THE PROJECT GUTENBERG EBOOK THE UNTOUCHABLE
ADOLESCENTS ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.