You are previewing Learn You a Haskell for Great Good!.
1. Learn You a Haskell for Great Good!
2. Introduction
3. 1. Starting Out
1. Calling Functions
2. Baby's First Functions
3. An Intro to Lists
4. Texas Ranges
5. I'm a List Comprehension
6. Tuples
4. 2. Believe the Type
1. Explicit Type Declaration
3. Type Variables
4. Type Classes 101
5. 3. Syntax in Functions
1. Pattern Matching
2. Guards, Guards!
3. where?!
4. let It Be
5. case Expressions
6. 4. Hello Recursion!
1. Maximum Awesome
2. A Few More Recursive Functions
3. Quick, Sort!
4. Thinking Recursively
7. 5. Higher-Order Functions
1. Curried Functions
2. Some Higher-Orderism Is in Order
3. The Functional Programmer's Toolbox
4. Lambdas
5. I Fold You So
6. Function Application with \$
7. Function Composition
8. 6. Modules
1. Importing Modules
2. Solving Problems with Module Functions
3. Mapping Keys to Values
4. Making Our Own Modules
9. 7. Making Our Own Types and Type Classes
1. Defining a New Data Type
2. Shaping Up
3. Record Syntax
4. Type Parameters
5. Derived Instances
6. Type Synonyms
7. Recursive Data Structures
8. Type Classes 102
9. A Yes-No Type Class
10. The Functor Type Class
11. Kinds and Some Type-Foo
10. 8. Input and Output
1. Separating the Pure from the Impure
2. Hello, World!
3. Gluing I/O Actions Together
4. Some Useful I/O Functions
5. I/O Action Review
11. 9. More Input and More Output
1. Files and Streams
3. To-Do Lists
4. Command-Line Arguments
5. More Fun with To-Do Lists
6. Randomness
7. Bytestrings
12. 10. Functionally Solving Problems
1. Reverse Polish Notation Calculator
2. Heathrow to London
13. 11. Applicative Functors
1. Functors Redux
2. Functor Laws
3. Using Applicative Functors
4. Useful Functions for Applicatives
14. 12. Monoids
1. Wrapping an Existing Type into a New Type
3. Meet Some Monoids
4. Folding with Monoids
15. 13. A Fistful of Monads
2. Getting Your Feet Wet with Maybe
4. Walk the Line
5. do Notation
16. 14. For a Few Monads More
1. Writer? I Hardly Knew Her!
2. Reader? Ugh, Not This Joke Again
3. Tasteful Stateful Computations
4. Error Error on the Wall
6. Making a Safe RPN Calculator
17. 15. Zippers
1. Taking a Walk
2. Focusing on Lists
3. A Very Simple Filesystem
18. Index
20. Colophon

Chapter 9. More Input and More Output

Now that you understand the concepts behind Haskell’s I/O, we can start doing fun stuff with it. In this chapter, we’ll interact with files, make random numbers, deal with command-line arguments, and more. Stay tuned!

Files and Streams

Armed with the knowledge about how I/O actions work, we can move on to reading and writing files with Haskell. But first, let’s take a look at how we can use Haskell to easily process streams of data. A stream is a succession of pieces of data entering or exiting a program over time. For instance, when you’re inputting characters into a program via the keyboard, those characters can be thought of as a stream.

Input Redirection

Many interactive programs get the user’s input via the ...