Book contents
- Frontmatter
- Contents
- Preface
- Chapter 1 Introduction
- Chapter 2 First steps
- Chapter 3 Types and classes
- Chapter 4 Defining functions
- Chapter 5 List comprehensions
- Chapter 6 Recursive functions
- Chapter 7 Higher-order functions
- Chapter 8 Functional parsers
- Chapter 9 Interactive programs
- Chapter 10 Declaring types and classes
- Chapter 11 The countdown problem
- Chapter 12 Lazy evaluation
- Chapter 13 Reasoning about programs
- Appendix A Standard prelude
- Appendix B Symbol table
- Bibliography
- Index
Chapter 9 - Interactive programs
- Frontmatter
- Contents
- Preface
- Chapter 1 Introduction
- Chapter 2 First steps
- Chapter 3 Types and classes
- Chapter 4 Defining functions
- Chapter 5 List comprehensions
- Chapter 6 Recursive functions
- Chapter 7 Higher-order functions
- Chapter 8 Functional parsers
- Chapter 9 Interactive programs
- Chapter 10 Declaring types and classes
- Chapter 11 The countdown problem
- Chapter 12 Lazy evaluation
- Chapter 13 Reasoning about programs
- Appendix A Standard prelude
- Appendix B Symbol table
- Bibliography
- Index
Summary
In this chapter we show how Haskell can be used to write interactive programs. We start by explaining what interactive programs are, show how such programs can naturally be viewed as functions, define a number of basic interactive programs and higher-order functions for combining interactive programs, and conclude by developing a calculator and the game of life.
Interaction
A batch program is one that does not interact with the user while it is running. In the early days of computing, most programs were batch programs, run in isolation from their users in order to maximise the amount of time that the computer was performing useful work. For example, a route-planning program may take start and finish points as its input, silently perform a large number of calculations, and then produce a recommended route as its output.
Up to this point in the book we have considered how Haskell can be used to write batch programs. In Haskell such programs, and more generally all programs, are modelled as pure functions that take all their input as explicit arguments, and produce all their output as explicit results. For example, a route planner may be modelled as a function of type (Point, Point) → Route that takes a pair of points and produces a route between them.
In contrast, an interactive program is one that may take additional input from the user, and produce additional output for the user, while the program is running.
- Type
- Chapter
- Information
- Programming in Haskell , pp. 87 - 98Publisher: Cambridge University PressPrint publication year: 2007