ML for the Working Programmer - GBV

29 downloads 1194 Views 205KB Size Report
ML for the Working Programmer. 2nd edition. Lawrence C. Paulson. University of Cambridge. CAMBRIDGE. UNIVERSITY PRESS ...
ML for the Working Programmer 2nd edition

Lawrence C. Paulson University of Cambridge

CAMBRIDGE UNIVERSITY PRESS

CONTENTS

Preface to the Second Edition

xiii

Preface

xv

1

Standard ML Functional Programming 1.1 Expressions versus commands 1.2 Expressions in procedural programming languages 1.3 Storage management 1.4 Elements of a functional language 1.5 The efficiency of functional programming Standard ML 1.6 The evolution of Standard ML 1.7 The ML tradition of theorem proving 1.8 The new standard library 1.9 ML and the working programmer

1 2 2 3 5 5 9 11 11 12 13 15

2

Names, Functions and Types Chapter outline Value declarations 2.1 Naming constants 2.2 Declaring functions 2.3 Identifiers in Standard ML Numbers, character strings and truth values 2.4 Arithmetic 2.5 Strings and characters 2.6 Truth values and conditional expressions Pairs, tuples and records 2.7 Vectors: an example of pairing 2.8 Functions with multiple arguments and results 2.9 Records

17 18 18 18 19 21 22 22 24 26 27 28 29 32

vi

Contents

2.10 Infix operators The evaluation of expressions 2.11 Evaluation in ML: call-by-value 2.12 Recursive functions under call-by-value 2.13 Call-by-need, or lazy evaluation Writing recursive functions 2.14 Raising to an integer power 2.15 Fibonacci numbers 2.16 Integer square roots Local declarations 2.17 Example: real square roots 2.18 Hiding declarations using l o c a l 2.19 Simultaneous declarations Introduction to modules 2.20 The complex numbers 2.21 Structures . 2.22 Signatures Polymorphic type checking 2.23 Type inference 2.24 Polymorphic function declarations Summary of main points Lists Chapter outline Introduction to lists Building a list 3.1 3.2 Operating on a list Some fundamental list functions 3.3 Testing lists and taking them apart 3.4 List processing by numbers 3.5 Append and reverse 3.6 Lists of lists, lists of pairs Applications of lists 3.7 Making change 3.8 Binary arithmetic 3.9 Matrix transpose 3.10 Matrix multiplication 3.11 Gaussian elimination 3.12 Writing a number as the sum of two squares

36 38 39 40 44 48 48 49 52 53 54 55 56 59 59 60 62 63 64 65 67 69 69 70 70 72 74 74 76 78 81 82 83 " 85 87 89 90 93

Contents

vn

3.13 The problem of the next permutation The equality test in polymorphic functions 3.14 Equality types 3.15 Polymorphic set operations 3.16 Association lists 3.17 Graph algorithms Sorting: A case study 3.18 Random numbers 3.19 Insertion sort 3.20 Quicksort 3.21 Merge sort Polynomial arithmetic 3.22 Representing abstract data 3.23 Representing polynomials 3.24 Polynomial addition and multiplication 3.25 The greatest common divisor Summary of main points 4

Trees and Concrete Data Chapter outline The d a t a t y p e declaration 4.1 The King and his subjects ; 4.2 Enumeration types 4.3 Polymorphic datatypes 4.4 Pattern-matching with v a l , a s , c a s e Exceptions . .:...• 4.5 Introduction to exceptions 4.6 Declaring exceptions -s 4.7 Raising exceptions 4.8 Handling exceptions 4.9 Objections to exceptions Trees : 4.10 A type for binary trees 4.11 Enumerating the contents of a tree ; •, 4.12 Building a tree from a list 4.13 A structure for binary trees Tree-based data structures 4.14 Dictionaries •'' 4.15 Functional and flexible arrays

95 96 97 98 101 102 108 108 109 110 111 114 115 116 117 119 121

J

'

123 123 124 124 127 128 130 134 134 135 136 138 140 141 142 145 146 148 148 149 154

viii

Contents

4.16 Priority queues A tautology checker 4.17 Propositional Logic 4.18 Negation normal form 4.19 Conjunctive normal form Summary of main points

, .

159 164 164 166 167 170

Functions and Infinite Data Chapter outline , Functions as values 5.1 Anonymous functions with fn notation 5.2 ^ Curried functions , 5.3 Functions in data structures 5.4 Functions as arguments and results General-purpose functionals 5.5 Sections 5.6 Combinators 5.7 The list functionals map and filter 5.8 The list functionals takewhile and dropwhile 5.9 The list functionals exists and all 5.10 The list functionals/