ebook img

Elementary standard ML PDF

310 Pages·1995·1.765 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Elementary standard ML

Elementary Standard ML Greg Michaelson Heriot-Watt University © Greg Michaelson 1995 This book is copyright under the Berne Convention. No reproduction without permission. All rights reserved. First published in 1995 by UCL Press UCL Press Limited University College London Gower Street London WC1E 6BT This edition published in the Taylor & Francis e-Library, 2005. “To purchase your own copy of this or any of Taylor & Francis or Routledge’s collection of thousands of eBooks please go to www.eBookstore.tandf.co.uk.” The name of University College London (UCL) is a registered trade mark used by UCL Press with the consent of the owner. ISBN 0-203-11659-3 Master e-book ISBN ISBN 0-203-16327-3 (Adobe eReader Format) ISBN: 1-85728-398-8 (Print Edition) PB British Library Cataloguing-in-Publication Data A CIP catalogue record for this book is available from the British Library. Contents Preface x Overview x Approach x Where to start? x Functional programming xi Why start with a functional language? xi Why Standard ML? xi Contents xii Acknowledgements xii 1 Introduction 1 1.1 Introduction 1 1.2 Making models 1 1.3 Things, collections and properties 2 1.4 Properties of properties 2 1.5 Types and methods 3 1.6 Choosing types 4 1.7 Characterizing things and collections 5 1.8 New types from old types 5 1.9 The story so far 6 1.10 Expressions from methods 6 1.11 Generalizing through naming 7 1.12 Specializing abstractions 7 1.13 Abstraction by comparison 8 1.14 Naming functions 9 1.15 Deciding what to do next 9 1.16 Names are arbitrary 12 1.17 New collections from old collections 13 1.18 Mapping 14 1.19 Filtering 15 1.20 Folding 17 1.21 Checking 19 iv 1.22 Higher order functions 20 1.23 Naming and recursion 23 1.24 Summary 24 1.25 Book structure 25 1.26 Syntax notation 25 1.27 Typefaces 25 1.28 Exercises 26 2 Basic types 28 2.1 Introduction 28 2.2 Expressions and types 28 2.3 Basic system use 28 2.4 Boolean type 28 2.5 Case sensitivity 29 2.6 Integer type 29 2.7 Real type 29 2.8 Numeric precision 30 2.9 String type 30 2.10 Tuple type 31 2.11 Function type overview 33 2.12 Boolean operators 34 2.13 Precedence and boolean expressions 35 2.14 Integer operators 36 2.15 Tuple type operators 39 2.16 Real operators 40 2.17 Overloaded operators 41 2.18 Mixed type arithmetic 41 2.19 String operators 42 2.20 Comparison operators 42 2.21 Boolean comparison 45 2.22 Real comparison 45 2.23 Division and remainder with a negative operand 46 2.24 Tuples with expressions 47 2.25 Function composition 47 2.26 Introducing exceptions 48 2.27 Summary 48 2.28 Exercises 48 3 Global declarations and functions 51 v 3.1 Introduction 51 3.2 Names 51 3.3 Reserved words 52 3.4 Global declarations 52 3.5 Functions 53 3.6 Naming functions 56 3.7 Comments 57 3.8 Making the bound variable type explicit 58 3.9 Abstraction over functions 58 3.10 Introducing higher order functions 63 3.11 Introducing polymorphism 67 3.12 Function composition function 70 3.13 Scope 72 3.14 Illustrating function application 73 3.15 Testing 73 3.16 Summary 75 3.17 Exercises 75 4 Pattern matching and recursion 80 4.1 Simplified notations 80 4.2 Tables and functions 81 4.3 Pattern matching 82 4.4 Pattern matching with strings 83 4.5 Raising exceptions 84 4.6 Wildcard pattern matching 85 4.7 Pattern matching with booleans 86 4.8 Recursion with integers 89 4.9 Generalizing summing integer sequences 92 4.10 Conditional expression 94 4.11 Conditional expressions and boolean values 95 4.12 Pattern matching with reals 96 4.13 More testing 97 4.14 Summary 99 4.15 Exercises 99 5 Introducing lists 104 5.1 Introduction 104 5.2 Basic list values 104 5.3 Pattern matching with lists 108 vi 5.4 Equality types 110 5.5 General list operations 112 5.5.1 Add to end of list 112 5.5.2 Append two lists end to end 112 5.5.3 Insert before value in list 114 5.5.4 Delete value from list 115 5.5.5 Replace value in list 116 5.6 Explicit list element selection 118 5.7 Indexed list access 119 5.8 Testing list functions 122 5.9 Summary 123 5.10 Exercises 123 6 List higher order functions 127 6.1 Accumulation variables 127 6.2 Encapsulation with local declarations 128 6.3 Ascending sequences 128 6.4 List reversal 129 6.5 List mapping 130 6.6 List filtering 132 6.7 Ordered lists 135 6.8 Insertion into ordered lists 137 6.9 Layered patterns 138 6.10 Insertion sort 139 6.11 Mapping over two lists 140 6.12 Merge sort 142 6.13 Folding lists of lists 143 6.14 Generalized folding 145 6.15 Testing groups of functions 146 6.16 Summary 147 6.17 Exercises 147 7 Tuple lists 152 7.1 Introduction 152 7.2 Pattern matching with tuples 152 7.3 Tuples for accumulation variables 154 7.4 Pattern matching with lists of tuples 155 7.5 Accumulation with lists 156 7.6 Generalized list find, delete and replace 157 vii 7.7 Example—telephone directory 159 7.8 Example—stock control 161 7.9 Type expressions and type abbreviations 165 7.10 Curried and uncurried functions 166 7.11 Summary 167 7.12 Exercises 168 8 Text processing 175 8.1 Introduction 175 8.2 String to list conversion 175 8.3 Text editing 175 8.3.1 Does one string start another? 175 8.3.2. Does one string contain another? 177 8.3.3 Delete one string from another 178 8.3.4 Insert one string before another in a third string 180 8.3.5 Insert one string after another in a third string 182 8.3.6 Replace one string with another in a third string 184 8.4 Digit string to number value conversion 185 8.5 Let expression 188 8.6 Unzip revisited 189 8.7 Finding words in a string 190 8.8 Counting words 193 8.9 Summary 194 8.10 Exercises 195 9 Concrete data types 197 9.1 Introduction 197 9.2 New types 197 9.3 Example—simple lexical analysis 198 9.4 Structured datatype binding 200 9.5 Structured pattern matching 201 9.6 Union types 203 9.7 Example—converting digit strings to number lists 205 9.8 Example—lexical analysis of arithmetic expressions 207 9.8.1 Introduction 207 9.8.2 Analyzer function 208 9.8.3 Mutual declarations 208 9.8.4 Recognizing identifiers 209 9.8.5 Recognizing integers 209 viii 9.8.6 Complete analyzer 210 9.8.7 Pretty printer 210 9.9 Pattern matching summary 211 9.10 Summary 211 9.11 Exercises 212 10 Defining lists and trees 216 10.1 Linked structures 216 10.2 String linked lists 217 10.3 Generalized lists 218 10.4 List efficiency 218 10.5 Introducing trees 219 10.6 Binary tree datatype 221 10.7 Polymorphic trees 224 10.8 Grammar and parsing 225 10.9 Parse trees 230 10.10 Concrete and abstract syntax 235 10.11 Parsing arithmetic expressions 236 10.12 Case expression 239 10.13 Multiple exceptions 240 10.14 Interpreting arithmetic expressions 241 10.15 Arithmetic pretty printer 242 10.16 Summary 242 10.17 Exercises 243 11 Input and output 247 11.1 Introduction 247 11.2 Unit type 247 11.3 Screen output 248 11.4 String list output 249 11.5 Integer list output 250 11.6 Formatted output 251 11.7 Keyboard input 253 11.8 Interactive I/O 254 11.9 Handling exceptions 256 11.10 File output 258 11.11 Sequenced file output 259 11.12 File input 261 11.13 Sequenced file input 262 ix 11.14 Summary 263 11.15 Exercises 263 12 Further SML 266 12.1 Introduction 266 12.2 Functional and imperative languages 266 12.2.1 Imperative aspects of SML 267 12.2.2 Replacing recursion with iteration 268 12.2.3 Replacing copying with assignment 271 12.3 Encapsulation with abstract types and modules 272 12.3.1 Abstract types 272 12.3.2 Structures 274 12.4 Other features 276 12.5 Further reading 276 12.6 SML implementations 277 Appendix Using an SML system 278 A: A.1 Introduction 278 A.2 Getting started 278 A.3 Leaving the system 279 A.4 Panic button 279 A.5 Program development 279 A.6 Saving system output 280 Appendix SML syntax 282 B: Appendix SML standard functions and operators 287 C: C.1 Standard functions 287 C.2 I/O streams and functions 287 C.3 Standard operators 288 Index 289

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.