You are previewing The Haskell School of Expression.
O'Reilly logo
The Haskell School of Expression

Book Description

Functional programming is a style of programming that emphasizes the use of functions (in contrast to object-oriented programming, which emphasizes the use of objects). It has become popular in recent years because of its simplicity, conciseness, and clarity. This book, first published in 2000, teaches functional programming as a way of thinking and problem solving, using Haskell, the most popular purely functional language. Rather than using the conventional (boring) mathematical examples commonly found in other programming language textbooks, the author uses examples drawn from multimedia applications, including graphics, animation, and computer music, thus rewarding the reader with working programs for inherently more interesting applications. Aimed at both beginning and advanced programmers, this tutorial begins with a gentle introduction to functional programming and moves rapidly on to more advanced topics. Details about progamming in Haskell are presented in boxes throughout the text so they can be easily found and referred to.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Contents
  5. Preface
  6. 1 Problem Solving, Programming, and Calculation
    1. 1.1 Computation by Calculation in Haskell
    2. 1.2 Expressions, Values, and Types
    3. 1.3 Function Types and Type Signatures
    4. 1.4 Abstraction, Abstraction, Abstraction
      1. 1.4.1 Naming
      2. 1.4.2 Functional Abstraction
      3. 1.4.3 Data Abstraction
    5. 1.5 Code Reuse and Modularity
    6. 1.6 Beware of Programming with Numbers
  7. 2 A Module of Shapes: Part I
    1. 2.1 Geometric Shapes
    2. 2.2 Areas of Shapes
    3. 2.3 Cleaning Up
  8. 3 Simple Graphics
    1. 3.1 Basic Input/Output
    2. 3.2 Graphics Windows
    3. 3.3 Drawing Graphics Other Than Text
    4. 3.4 Some Examples
  9. 4 Shapes II: Drawing Shapes
    1. 4.1 Dealing With Different Coordinate Systems
    2. 4.2 Converting Shapes to Graphics
    3. 4.3 Some Examples
    4. 4.4 In Retrospect
  10. 5 Polymorphic and Higher-Order Functions
    1. 5.1 Polymorphic Types
    2. 5.2 Abstraction Over Recursive Definitions
      1. 5.2.1 Map is Polymorphic
      2. 5.2.2 Using map
    3. 5.3 Append
      1. 5.3.1 The Efficiency and Fixity of Append
    4. 5.4 Fold
      1. 5.4.1 Haskell’s Folds
      2. 5.4.2 Why Two Folds?
    5. 5.5 A Final Example: Reverse
    6. 5.6 Errors
  11. 6 Shapes III: Perimeters of Shapes
    1. 6.1 Perimeters of Shapes
  12. 7 Trees
    1. 7.1 A Tree Data Type
    2. 7.2 Operations on Trees
    3. 7.3 Arithmetic Expressions
  13. 8 A Module of Regions
    1. 8.1 The Region Data Type
    2. 8.2 The Meaning of Shapes and Regions
      1. 8.2.1 The Meaning of Shapes
      2. 8.2.2 The Encoding of the Meaning of Shapes
      3. 8.2.3 The Meaning of Regions
      4. 8.2.4 The Encoding of the Meaning of Regions
    3. 8.3 Algebraic Properties of Regions
    4. 8.4 In Retrospect
  14. 9 More About Higher-Order Functions
    1. 9.1 Currying
    2. 9.2 Sections
    3. 9.3 Anonymous Functions
    4. 9.4 Function Composition
  15. 10 Drawing Regions
    1. 10.1 The Picture Data Type
    2. 10.2 Drawing Pictures
    3. 10.3 Drawing Regions
      1. 10.3.1 From Regions to Graphics Regions: First Attempt
      2. 10.3.2 From Regions to Graphics Regions: Second Attempt
      3. 10.3.3 Translating Shapes into Graphics Regions
      4. 10.3.4 Examples
    4. 10.4 User Interaction
    5. 10.5 Putting it all Together
      1. 10.5.1 Examples
  16. 11 Proof by Induction
    1. 11.1 Induction and Recursion
    2. 11.2 Examples of List Induction
      1. 11.2.1 Proving Function Equivalences
    3. 11.3 Useful Properties on Lists
      1. 11.3.1 Function Strictness
    4. 11.4 Induction on Other Data Types
      1. 11.4.1 A More Efficient Exponentiation Function
  17. 12 Qualified Types
    1. 12.1 Equality
    2. 12.2 Defining Your Own Type Classes
    3. 12.3 Inheritance
    4. 12.4 Haskell’s Standard Type Classes
    5. 12.5 Derived Instances
    6. 12.6 Reasoning With Type Classes
  18. 13 A Module of Simple Animations
    1. 13.1 What is an Animation?
    2. 13.2 Representing an Animation
    3. 13.3 An Animator
    4. 13.4 Fun With Type Classes
      1. 13.4.1 Rising to the Level of Animations
      2. 13.4.2 Type Classes to the Rescue
      3. 13.4.3 Defining New Type Classes for Behaviors
    5. 13.5 Lifting to the Limit
    6. 13.6 Time Transformation
    7. 13.7 A Final Example: A Kaleidoscope Program
  19. 14 Programming With Streams
    1. 14.1 Lazy Evaluation
    2. 14.2 Recursive Streams
    3. 14.3 Stream Diagrams
    4. 14.4 Lazy Patterns
    5. 14.5 Memoization
    6. 14.6 Inductive Properties of Infinite Lists
  20. 15 A Module of Reactive Animations
    1. 15.1 FAL by Example
      1. 15.1.1 Basic Reactivity
      2. 15.1.2 Event Choice
      3. 15.1.3 Recursive Event Processing
      4. 15.1.4 Events with Data
      5. 15.1.5 Snapshot
      6. 15.1.6 Boolean Events
      7. 15.1.7 Integration
    2. 15.2 Implementing FAL
      1. 15.2.1 An Implementation Strategy
      2. 15.2.2 Incremental Sampling
      3. 15.2.3 Final Refinements
      4. 15.2.4 Representing Events
    3. 15.3 The Implementation
      1. 15.3.1 Behaviors
      2. 15.3.2 Events
      3. 15.3.3 An Example
    4. 15.4 Extensions
      1. 15.4.1 Variations on switch
      2. 15.4.2 Mouse Motion
    5. 15.5 Paddleball in Twenty Lines
  21. 16 Communicating With the Outside World
    1. 16.1 Files, Channels, and Handles
      1. 16.1.1 Why Use Handles?
      2. 16.1.2 Channels
    2. 16.2 Exception Handling
    3. 16.3 First-Class Channels and Concurrency
  22. 17 Rendering Reactive Animations
    1. 17.1 Preliminaries
    2. 17.2 Reactimate
    3. 17.3 Window User
  23. 18 Higher-Order Types
    1. 18.1 The Functor Class
    2. 18.2 The Monad Class
      1. 18.2.1 Other Instances of Monad
      2. 18.2.2 Other Monadic Operations
    3. 18.3 The MonadPlus Class
    4. 18.4 State Monads
    5. 18.5 Type Class Type Errors
  24. 19 An Imperative Robot Language
    1. 19.1 IRL by Example
    2. 19.2 Robot is a State Monad
    3. 19.3 The Implementation of IRL Commands
      1. 19.3.1 Robot Orientation
      2. 19.3.2 Using the Pen
      3. 19.3.3 Playing With Coins
      4. 19.3.4 Logic and Control
    4. 19.4 All the World is a Grid
    5. 19.5 Robot Graphics
    6. 19.6 Putting it all Together
  25. 20 Functional Music Composition
    1. 20.1 The Music Data Type
    2. 20.2 Higher-Level Constructions
      1. 20.2.1 Lines and Chords
      2. 20.2.2 Delay and Repeat
      3. 20.2.3 Polyrhythms
      4. 20.2.4 Determining Duration
      5. 20.2.5 Reversing Musical Structure
      6. 20.2.6 Truncating Parallel Composition
      7. 20.2.7 Trills
      8. 20.2.8 Percussion
    3. 20.3 A Couple of Final Examples
      1. 20.3.1 Cascades
      2. 20.3.2 Self-Similar (Fractal) Music
  26. 21 Interpreting Functional Music
    1. 21.1 Interpreting Music: A Performance
    2. 21.2 An Algebra of Music
  27. 22 From Performance to MIDI
    1. 22.1 An Introduction to MIDI
    2. 22.2 The Conversion Process
    3. 22.3 Putting It All Together
  28. 23 A Tour of the PreludeList Module
    1. 23.1 The PreludeList Module
    2. 23.2 Simple List Selector Functions
    3. 23.3 Index-Based Selector Functions
    4. 23.4 Predicate-Based Selector Functions
    5. 23.5 Fold-like Functions
    6. 23.6 List Generators
    7. 23.7 String-Based Functions
    8. 23.8 Boolean List Functions
    9. 23.9 List Membership Functions
    10. 23.10 Arithmetic on Lists
    11. 23.11 List Combining Functions
  29. 24 A Tour of Haskell’s Standard Type Classes
    1. 24.1 The Ordered Class
    2. 24.2 The Enumeration Class
    3. 24.3 The Bounded Class
    4. 24.4 The Show Class
    5. 24.5 The Read Class
    6. 24.6 The Index Class
    7. 24.7 The Numeric Classes
  30. A Built-in Types Are Not Special
  31. B Pattern-Matching Details
  32. Bibliography
  33. Index