logo资料库

Programming in Haskell(PDF).pdf

第1页 / 共184页
第2页 / 共184页
第3页 / 共184页
第4页 / 共184页
第5页 / 共184页
第6页 / 共184页
第7页 / 共184页
第8页 / 共184页
资料共184页,剩余部分请下载后查看
Half-title
Title
Copyright
Dedication
Contents
Preface
Chapter 1 Introduction
1.1 Functions
1.2 Functional programming
1.3 Features of Haskell
1.4 Historical background
1.5 A taste of Haskell
1.6 Chapter remarks
1.7 Exercises
Chapter 2 First steps
2.1 The Hugs system
2.2 The standard prelude
2.3 Function application
2.4 Haskell scripts
My first script
Naming requirements
The layout rule
Comments
2.5 Chapter remarks
2.6 Exercises
Chapter 3 Types and classes
3.1 Basic concepts
3.2 Basic types
Bool – logical values
Char – single characters
String – strings of characters
Int – fixed-precision integers
Integer – arbitrary-precision integers
Float – single-precision floating-point numbers
3.3 List types
3.4 Tuple types
3.5 Function types
3.6 Curried functions
3.7 Polymorphic types
3.8 Overloaded types
3.9 Basic classes
Eq – equality types
Ord – ordered types
Show – showable types
Read – readable types
Num – numeric types
Integral – integral types
Fractional – fractional types
3.10 Chapter remarks
3.11 Exercises
Chapter 4 Defining functions
4.1 New from old
4.2 Conditional expressions
4.3 Guarded equations
4.4 Pattern matching
Tuple patterns
List patterns
Integer patterns
4.5 Lambda expressions
4.6 Sections
4.7 Chapter remarks
4.8 Exercises
Chapter 5 List comprehensions
5.1 Generators
5.2 Guards
5.3 The zip function
5.4 String comprehensions
5.5 The Caesar cipher
Encoding and decoding
Frequency tables
Cracking the cipher
5.6 Chapter remarks
5.7 Exercises
Chapter 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
Example – product
Step 1: define the type
Step 2: enumerate the cases
Step 3: define the simple cases
Step 4: define the other cases
Step 5: generalise and simplify
Example – drop
Step 1: define the type
Step 2: enumerate the cases
Step 3: define the simple cases
Step 4: define the other cases
Step 5: generalise and simplify
Example – init
Step 1: define the type
Step 2: enumerate the cases
Step 3: define the simple cases
Step 4: define the other cases
Step 5: generalise and simplify
6.7 Chapter remarks
6.8 Exercises
Chapter 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 String transmitter
Binary numbers
Base conversion
Transmission
7.7 Chapter remarks
7.8 Exercises
Chapter 8 Functional parsers
8.1 Parsers
8.2 The parser type
8.3 Basic parsers
8.4 Sequencing
8.5 Choice
8.6 Derived primitives
8.7 Handling spacing
8.8 Arithmetic expressions
8.9 Chapter remarks
8.10 Exercises
Chapter 9 Interactive programs
9.1 Interaction
9.2 The input/output type
9.3 Basic actions
9.4 Sequencing
9.5 Derived primitives
9.6 Calculator
9.7 Game of life
9.8 Chapter remarks
9.9 Exercises
Chapter 10 Declaring types and classes
10.1 Type declarations
10.2 Data declarations
10.3 Recursive types
10.4 Tautology checker
10.5 Abstract machine
10.6 Class and instance declarations
Derived instances
Monadic types
10.7 Chapter remarks
10.8 Exercises
Chapter 11 The countdown problem
11.1 Introduction
11.2 Formalising the problem
11.3 Brute force solution
11.4 Combining generation and evaluation
11.5 Exploiting algebraic properties
11.6 Chapter remarks
11.7 Exercises
Chapter 12 Lazy evaluation
12.1 Introduction
12.2 Evaluation strategies
Lambda expressions
12.3 Termination
12.4 Number of reductions
12.5 Infinite structures
12.6 Modular programming
12.7 Strict application
12.8 Chapter remarks
12.9 Exercises
Chapter 13 Reasoning about programs
13.1 Equational reasoning
13.2 Reasoning about Haskell
13.3 Simple examples
13.4 Induction on numbers
13.5 Induction on lists
13.6 Making append vanish
13.7 Compiler correctness
13.8 Chapter remarks
13.9 Exercises
Appendix A Standard prelude
A.1 Classes
A.2 Logical values
A.3 Characters and strings
A.4 Numbers
A.5 Tuples
A.6 Maybe
A.7 Lists
A.8 Functions
A.9 Input/output
Appendix B Symbol table
Bibliography
Index
This page intentionally left blank
Programming in Haskell Haskell is one of the leading languages for teaching functional programming, enabling students to write simpler and cleaner code, and to learn how to structure and reason about programs. This introduction is ideal for beginners: it requires no previous programming experience and all concepts are explained from first principles with the aid of carefully chosen examples. Each chapter includes a series of exercises, ranging from the straightforward to extended projects, along with suggestions for further reading on more advanced topics. The presentation is clear and simple, and benefits from having been refined and class-tested over several years. Features: r Powerpoint slides for each chapter freely available for instructors and students from the book’s website r Solutions to exercises, and examination questions (with solutions) available to instructors r All the code in the book is fully compliant with the latest release of Haskell, and can be downloaded from the web. r Written by a leading Haskell researcher and instructor, well known for his teach- ing skills r Can be used with courses, or as a stand-alone text for self-learning Graham Hutton has worked in four of the leading centres for research and teaching on functional programming. He has more than 15 years of experience in functional programming research, during which time he has published more than 30 research articles, chaired the Haskell Workshop, and edited a special issue on Haskell of the Journal of Functional Programming. He also has more than 10 years’ experience in teaching Haskell, and in promoting the use of functional programming in the curriculum.
Programming in Haskell Graham Hutton University of Nottingham
CAMBRIDGE UNIVERSITY PRESS Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, São Paulo Cambridge University Press The Edinburgh Building, Cambridge CB2 8RU, UK Published in the United States of America by Cambridge University Press, New York www.cambridge.org Information on this title: www.cambridge.org/9780521871723 © G. Hutton 2007 This publication is in copyright. Subject to statutory exception and to the provision of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press. First published in print format 2006 ISBN-13 978-0-511-29615-4 ISBN-10 0-511-29615-0 eBook (NetLibrary) eBook (NetLibrary) ISBN-13 978-0-521-87172-3 ISBN-10 0-521-87172-7 hardback hardback ISBN-13 978-0-521-69269-4 ISBN-10 0-521-69269-5 paperback paperback Cambridge University Press has no responsibility for the persistence or accuracy of urls for external or third-party internet websites referred to in this publication, and does not guarantee that any content on such websites is, or will remain, accurate or appropriate.
For Annette, Callum and Tom
Contents Preface Chapter 1 Introduction 1.1 Functions 1.2 Functional programming 1.3 Features of Haskell 1.4 Historical background 1.5 A taste of Haskell 1.6 Chapter remarks 1.7 Exercises Chapter 2 First steps 2.1 The Hugs system 2.2 The standard prelude 2.3 Function application 2.4 Haskell scripts 2.5 Chapter remarks 2.6 Exercises Chapter 3 Types and classes 3.1 Basic concepts 3.2 Basic types 3.3 List types 3.4 Tuple types 3.5 Function types 3.6 Curried functions 3.7 Polymorphic types 3.8 Overloaded types 3.9 Basic classes 3.10 Chapter remarks 3.11 Exercises Chapter 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 Sections 4.7 Chapter remarks 4.8 Exercises page xi 1 1 2 4 6 6 9 9 10 10 10 12 13 16 16 17 17 18 20 20 21 21 23 23 24 28 28 30 30 31 31 32 34 36 36 37
分享到:
收藏