Collections, text and laziness
These fourteen lessons collect the containers and the text types of Haskell, and then the laziness that makes them cheap: Data.Map and Data.Set, an IORef, an array, Data.Text and ByteString. A thunk and an infinite list follow, then seq and the bang pattern, a space leak read in +RTS -s, and the formatting and the arguments of a program.
5.1
Data.Map
fromList, lookup, insert, adjust, toList, the cost of each
Open
5.2
Data.Set
fromList, member, union, intersection, difference, a visited set
Open
5.3
IORef
newIORef, readIORef, writeIORef, modifyIORef', a counter across calls
Open
5.4
Arrays
Data.Array, listArray, (!), bounds, //, an immutable lookup table
Open
5.5
Data.Text
pack and unpack, splitOn, toUpper, Text against String, the API to reach for
Open
5.6
ByteString
pack, unpack, hPut, length in bytes, a file copied byte for byte
Open
5.7
Laziness and infinite lists
a thunk, take from an infinite list, cycle, repeat, what is evaluated when
Open
5.8
seq, $! and bang patterns
seq, $!, BangPatterns, deepseq, a fold that stops building thunks
Open
5.9
Space leaks and +RTS -s
foldl against foldl', +RTS -s with real numbers, the maximum residency line
Open
5.10
Random numbers without the random package
a hand-written linear congruential generator, a range, why random is not here
Open
5.11
Formatting output
Text.Printf, showFFloat, a padded column, a report
Open
5.12
Arguments and exit codes
getArgs, exitWith, exitFailure, a usage message, a bad argument
Open
5.13
Modules and exports
module X (a, B (..)) where, export lists, import qualified, hiding, re-exporting
Open
5.14
A program in several modules
a library module, a main module, ghc Main.hs building both, the artefact names
Open