Contents
Contents
1 Standard ML 1
Functional Programming 2
Standard ML 11
3 Lists 69
Chapter outline 69
Introduction to lists 70
Some fundamental list functions 74
Applications of lists 82
The equality test in polymorphic functions 96
Sorting: A case study 107
Polynomial arithmetic 114
Summary of main points 121
v
vi Contents
With each reprinting of this book, a dozen minor errors have silently disap-
peared. But a reprinting is no occasion for making improvements, however
valuable, that would affect the page numbering: we should then have several
slightly different, incompatible editions. An accumulation of major changes
(and the Editor’s urgings) have prompted this second edition.
As luck would have it, changes to ML have come about at the same time. ML
has a new standard library and the language itself has been revised. It is worth
stressing that the changes do not compromise ML’s essential stability. Some ob-
scure technical points have been simplified. Anomalies in the original definition
have been corrected. Existing programs will run with few or no changes. The
most visible changes are the new character type and a new set of top level library
functions.
The new edition brings the book up to date and greatly improves the presen-
tation. Modules are now introduced early — in Chapter 2 instead of Chapter 7
— and used throughout. This effects a change of emphasis, from data structures
(say, binary search trees) to abstract types (say, dictionaries). A typical section
introduces an abstract type and presents its ML signature. Then it explains the
ideas underlying the implementation, and finally presents the code as an ML
structure. Though reviewers have been kind to the first edition, many readers
have requested such a restructuring.
The programs have not just been moved about, but rewritten. They now reflect
modern thoughts on how to use modules. The open declaration, which obscures
a program’s modular structure, seldom appears. Functors are only used where
necessary. Programs are now indented with greater care. This, together with the
other changes, should make them much more readable than hitherto. They are
also better: there is a faster merge sort and simpler, faster priority queues.
The new standard library would in any case have necessitated an early men-
tion of modules. Although it entails changes to existing code, the new library
brings ML firmly into the fold of realistic languages. The library has been de-
signed, through a long process of consultation, to provide comprehensive sup-
port without needless complication. Its organization demonstrates the benefits
ix
x Preface to the Second Edition
Overview of the book. Most chapters are devoted to aspects of ML. Chapter 1
introduces the ideas behind functional programming and surveys the history of
ML . Chapters 2–5 cover the functional part of ML , including an introduction
to modules. Basic types, lists, trees and higher-order functions are presented.
Broader principles of functional programming are discussed.
Chapter 6 presents formal methods for reasoning about functional programs.
If this seems to be a distraction from the main business of programming, con-
sider that a program is worth little unless it is correct. Ease of formal reasoning
is a major argument in favour of functional programming.
xi
xii Preface
ML is not perfect. Certain pitfalls can allow a simple coding error to waste
hours of a programmer’s time. The new standard library introduces incompat-
ibilities between old and new compilers. Warnings of possible hazards appear
throughout the book. They look like this:
Beware the Duke of Gloucester. O Buckingham! take heed of yonder dog.
Look, when he fawns, he bites; and when he bites, his venom tooth will rankle
to the death. Have not to do with him, beware of him; Sin, Death, and Hell have set
their marks on him, and all their ministers attend on him.
I hasten to add that nothing in ML can have consequences quite this dire.
No fault in a program can corrupt the ML system itself. On the other hand,
programmers must remember that even correct programs can do harm in the
outside world.
book were developed under Moscow ML, Poly/ML and Standard ML of New
Jersey. I have not tried the other compilers.
To obtain MLWorks, contact Harlequin Limited, Barrington Hall, Barrington,
Cambridge, CB2 5RG, England. Their email address is [email protected].
To obtain Moscow ML, contact Peter Sestoft, Mathematical Section, Royal
Veterinary and Agricultural University, Thorvaldsensvej 40, DK-1871 Frede-
riksberg C, Denmark. Or get the system from the World Wide Web:
http:/www.dina.kvl.dk/˜sestoft/mosml.html
The programs in this book and answers to some exercises are available by
email; my address is [email protected]. If possible, please use the World
Wide Web; my home page is at
http://www.cl.cam.ac.uk/users/lcp/
Acknowledgements. The editor, David Tranah, assisted with all stages of the
writing and suggested the title. Graham Birtwistle, Glenn Bruns and David
Wolfram read the text carefully. Dave Berry, Simon Finn, Mike Fourman, Kent
Karlsson, Robin Milner, Richard O’Keefe, Keith van Rijsbergen, Nick Roth-
well, Mads Tofte, David N. Turner and the staff of Harlequin also commented
on the text. Andrew Appel, Gavin Bierman, Phil Brabbin, Richard Brooksby,
Guy Cousineau, Lal George, Mike Gordon, Martin Hansen, Darrell Kindred,
Silvio Meira, Andrew Morris, Khalid Mughal, Tobias Nipkow, Kurt Olender,
Allen Stoughton, Reuben Thomas, Ray Toal and Helen Wilson found errors in
previous printings. Piete Brooks, John Carroll and Graham Titmus helped with
the computers. I wish to thank Dave Matthews for developing Poly/ML, which
was for many years the only efficient implementation of Standard ML.
Of the many works in the bibliography, Abelson and Sussman (1985), Bird
xiv Preface
and Wadler (1988) and Burge (1975) have been especially helpful. Reade (1989)
contains useful ideas for implementing lazy lists in ML.
The Science and Engineering Research Council has supported LCF and ML in
numerous research grants over the past 20 years.
I wrote most of this book while on leave from the University of Cambridge.
I am grateful to the Computer Laboratory and Clare College for granting leave,
and to the University of Edinburgh for accommodating me for six months.
Finally, I should like to thank Sue for all she did to help, and for tolerating
my daily accounts of the progress of every chapter.