Table of Contents

Copyright

Brief Table of Contents

Table of Contents

Preface

Acknowledgments

About this book

About the author

Lesson 1. Getting started with Haskell

1.1. Welcome to Haskell

1.1.1. The Haskell Platform

1.1.2. Text editors

1.2. The Glasgow Haskell Compiler

1.3. Interacting with Haskell—GHCi

1.4. Writing and working with Haskell code

Summary

Unit 1. Foundations of functional programming

Lesson 2. Functions and functional programming

2.1. Functions

2.2. Functional programming

2.3. The value of functional programming in practice

2.3.1. Variables

2.3.2. Variables that are variable

Summary

Lesson 3. Lambda functions and lexical scope

3.1. Lambda functions

3.2. Writing your own where clause

3.3. From lambda to let: making your own variable variables!

3.4. Practical lambda functions and lexical scope

Summary

Lesson 4. First-class functions

4.1. Functions as arguments

4.1.1. Lambda functions as arguments

4.1.2. Example—custom sorting

4.2. Returning functions

Summary

Lesson 5. Closures and partial application

5.1. Closures—creating functions with functions

5.2. Example: Generating URLs for an API

5.2.1. Partial application: making closures simple

5.3. Putting it all together

Summary

Lesson 6. Lists

6.1. The anatomy of a list

6.2. Lists and lazy evaluation

6.3. Common functions on lists

6.3.1. The !! operator

6.3.2. length

6.3.3. reverse

6.3.4. elem

6.3.5. take and drop

6.3.6. zip

6.3.7. cycle

Summary

Lesson 7. Rules for recursion and pattern matching

7.1. Recursion

7.2. Rules for recursion

7.2.1. Rule 1: Identify the end goal(s)

7.2.2. Rule 2: Determine what happens when a goal is reached

7.2.3. Rule 3: List all alternate possibilities

7.2.4. Rule 4: Determine your “Rinse and Repeat”

7.2.5. Rule 5: Ensure that each alterative moves you toward the goal

7.3. Your first recursive function: greatest common divisor

Summary

Lesson 8. Writing recursive functions

8.1. Review: Rules of recursion

8.2. Recursion on lists

8.2.1. Implementing length

8.2.2. Implementing take

8.2.3. Implementing cycle

8.3. Pathological recursion: Ackerman function and the Collatz conjecture

8.3.1. The Ackermann function

8.3.2. The Collatz conjecture

Summary

Lesson 9. Higher-order functions

9.1. Using map

9.2. Abstracting away recursion with map

9.3. Filtering a list

9.4. Folding a list

Summary

Lesson 10. Capstone: Functional object-oriented programming with robots!

10.1. An object with one property: a cup of coffee

10.1.1. Creating a constructor

10.1.2. Adding accessors to your object

10.2. A more complex object: let’s build fighting robots!

10.2.1. Sending messages between objects

10.3. Why stateless programming matters

10.4. Types—objects and so much more!

Summary

Extending the exercise

Unit 2. Introducing types

Lesson 11. Type basics

11.1. Types in Haskell

11.2. Function types

11.2.1. Functions for converting to and from strings

11.2.2. Functions with multiple arguments

11.2.3. Types for first-class functions

11.3. Type variables

Summary

Lesson 12. Creating your own types

12.1. Using type synonyms

12.2. Creating new types

12.3. Using record syntax

Summary

Lesson 13. Type classes

13.1. Further exploring types

13.2. Type classes

13.3. The benefits of type classes

13.4. Defining a type class

13.5. Common type classes

13.6. The Ord and Eq type classes

13.6.1. Bounded

13.6.2. Show

13.7. Deriving type classes

Summary

Lesson 14. Using type classes

14.1. A type in need of classes

14.2. Implementing Show

14.3. Type classes and polymorphism

14.4. Default implementation and minimum complete definitions

14.5. Implementing Ord

14.6. To derive or not to derive?

14.7. Type classes for more-complex types

14.8. Type class roadmap

Summary

Lesson 15. Capstone: Secret messages!

15.1. Ciphers for beginners: ROT13

15.1.1. Implementing your own ROT cipher

15.1.2. The rotN algorithm

15.1.3. Rot encoding a string

15.1.4. The problem with decoded odd-sized alphabets

15.2. XOR: The magic of cryptography!

15.3. Representing values as bits

15.4. The one-time pad

15.4.1. Implementing your one-time pad

15.5. A Cipher class

Summary

Extending the exercise

Unit 3. Programming in types

Lesson 16. Creating types with “and” and “or”

16.1. Product types—combining types with “and”

16.1.1. The curse of product types: hierarchical design

16.2. Sum types—combining types with “or”

16.3. Putting together your bookstore

Summary

Lesson 17. Design by composition—Semigroups and Monoids

17.1. Intro to composability—combining functions

17.2. Combining like types: Semigroups

17.2.1. The Color Semigroup

17.2.2. Making Color associative and using guards

17.3. Composing with identity: Monoids

17.3.1. mconcat: Combining multiple Monoids at once

17.3.2. Monoid laws

17.3.3. Practical Monoids—building probability tables

Summary

Lesson 18. Parameterized types

18.1. Types that take arguments

18.1.1. A more useful parameterized type: Triple

18.1.2. Lists

18.2. Types with more than one parameter

18.2.1. Tuples

18.2.2. Kinds: types of types

18.2.3. Data.Map

Summary

Lesson 19. The Maybe type: dealing with missing values

19.1. Introducing Maybe: solving missing values with types

19.2. The problem with null

19.2.1. Handling missing values with errors

19.2.2. Returning null values

19.2.3. Using Maybe as a solution to missing values

19.3. Computing with Maybe

19.4. Back to the lab! More-complex computation with Maybe

Summary

Lesson 20. Capstone: Time series

20.1. Your data and the TS data type

20.1.1. Building a basic time-series type

20.2. Stitching together TS data with Semigroup and Monoid

20.2.1. Making TS an instance of Monoid

20.3. Performing calculations on your time series

20.3.1. Calculating the min and max values for your time series

20.4. Transforming time series

20.4.1. Moving average

Summary

Extending the exercise

Unit 4. IO in Haskell

Lesson 21. Hello World!—introducing IO types

21.1. IO types—dealing with an impure world

21.1.1. Examples of IO actions

21.1.2. Keeping values in the context of IO

21.2. Do-notation

21.3. An example: command-line pizza cost calculator

21.3.1. A peek at Monad—do-notation in Maybe

21.4. Summary

Lesson 22. Interacting with the command line and lazy I/O

22.1. Interacting with the command line the nonlazy way

22.2. Interacting with lazy I/O

22.2.1. Thinking of your problem as a lazy list

Summary

Lesson 23. Working with text and Unicode

23.1. The Text type

23.1.1. When to use Text vs. String

23.2. Using Data.Text

23.2.1. OverloadedStrings and Haskell extensions

23.2.2. Basic Text utilities

23.3. Text and Unicode

23.3.1. Searching Sanskrit

23.4. Text I/O

Summary

Lesson 24. Working with files

24.1. Opening and closing files

24.2. Simple I/O tools

24.3. The trouble with lazy I/O

24.4. Strict I/O

24.4.1. When to use lazy vs. strict

Summary

Lesson 25. Working with binary data

25.1. Working with binary data by using ByteString

25.2. Glitching JPEGs

25.2.1. Inserting random bytes

25.2.2. Sorting random bytes

25.2.3. Chaining together IO actions with foldM

25.3. ByteStrings, Char8, and Unicode

Summary

Lesson 26. Capstone: Processing binary files and book data

26.1. Working with book data

26.2. Working with MARC records

26.2.1. Understanding the structure of a MARC record

26.2.2. Getting the data

26.2.3. Checking the leader and iterating through your records

26.2.4. Reading the directory

26.2.5. Using the directory to look up fields

26.2.6. Processing the directory entries and looking up MARC fields

26.2.7. Getting Author and Title information from a MARC field

26.3. Putting it all together

Summary

Extending the exercise

Unit 5. Working with type in a context

Lesson 27. The Functor type class

27.1. An example: computing in a Maybe

27.2. Using functions in context with the Functor type class

27.3. Functors are everywhere!

27.3.1. One interface for four problems

27.3.2. Converting a Maybe RobotPart to Maybe Html

27.3.3. Converting a list of RobotParts to a list of HTML

27.3.4. Converting a Map of RobotParts to HTML

27.3.5. Transforming an IO RobotPart into IO Html

Summary

Lesson 28. A peek at the Applicative type class: using functions in a context

28.1. A command-line application for calculating the distance between cities

28.1.1. The limitations of Functor

28.2. Using <*> for partial application in a context

28.2.1. Introducing the <*> operator

28.2.2. Using <*> to finish your city distance program

28.2.3. Using a multi-argument function in IO using <$> and <*>

28.3. Using <*> to create data in a context

28.3.1. Creating a user in the context of a Maybe

Summary

Lesson 29. Lists as context: a deeper look at the Applicative type class

29.1. Introducing the Applicative type class

29.1.1. The pure method

29.2. Containers vs. contexts

29.3. List as a context

29.3.1. Exploring container vs. context with a list

29.3.2. A game show example

29.3.3. Generating the first N prime numbers

29.3.4. Quickly generating large amounts of test data

Summary

Lesson 30. Introducing the Monad type class

30.1. The limitations of Applicative and Functor

30.1.1. Combining two Map lookups

30.1.2. Writing a not-so-trivial echo IO action

30.2. The bind operator: >>=

30.3. The Monad type class

30.3.1. Using Monad to build a Hello <Name> program

Summary

Lesson 31. Making Monads easier with do-notation

31.1. Do-notation revisited

31.2. Using do-notation to reuse the same code in different contexts

31.2.1. The problem setup

31.2.2. The IO context—building a command-line tool

31.2.3. The Maybe context—working with a map of candidates

31.2.4. The List context—processing a list of candidates

31.2.5. Putting it all together and writing a monadic function

Summary

Lesson 32. The list monad and list comprehensions

32.1. Building lists with the list monad

32.1.1. The guard function

32.2. List comprehensions

32.3. Monads: much more than just lists

Summary

Lesson 33. Capstone: SQL-like queries in Haskell

33.1. Getting started

33.2. Basic queries for your list: select and where

33.2.1. Implementing _select

33.2.2. Implementing _where

33.3. Joining Course and Teacher data types

33.4. Building your HINQ interface and example queries

33.5. Making a HINQ type for your queries

33.6. Running your HINQ queries

33.6.1. Using HINQ with Maybe types

33.6.2. Joining multiple lists to get all enrollments

Summary

Extending the exercise

Unit 6. Organizing code and building projects

Lesson 34. Organizing Haskell code with modules

34.1. What happens when you write a function with the same name as one in Prelude?

34.2. Building a multifile program with modules

34.2.1. Creating the Main module

34.2.2. Putting your improved isPalindrome code in its own module

34.2.3. Using your Palindrome module in your Main module

Summary

Lesson 35. Building projects with stack

35.1. Starting a new stack project

35.2. Understanding the project structure

35.2.1. The project .cabal file and autogenerated files

35.2.2. The app, src, and test directories

35.3. Writing your code

35.4. Building and running your project!

35.4.1. A quick improvement: getting rid of language pragmas

Summary

Lesson 36. Property testing with QuickCheck

36.1. Starting a new project

36.2. Different types of testing

36.2.1. Manual testing and calling GHCi from stack

36.2.2. Writing your own unit tests and using stack test

36.3. Property testing QuickCheck

36.3.1. Testing properties

36.3.2. Introducing QuickCheck

36.3.3. Using QuickCheck with more types and installing packages

Summary

Lesson 37. Capstone: Building a prime-number library

37.1. Starting your new project

37.2. Modifying the default files

37.3. Writing your core library functions

37.3.1. Defining primes

37.3.2. Defining an isPrime function

37.4. Writing tests for your code

37.4.1. Defining properties for isPrime

37.4.2. Fixing the bug

37.5. Adding code to factor numbers

Summary

Extending the exercise

Unit 7. Practical Haskell

Lesson 38. Errors in Haskell and the Either type

38.1. Head, partial functions, and errors

38.1.1. Head and partial functions

38.2. Handling partial functions with Maybe

38.3. Introducing the Either type

38.3.1. Building a prime check with Either

Summary

Lesson 39. Making HTTP requests in Haskell

39.1. Getting your project set up

39.1.1. Your starter code

39.2. Using the HTTP.Simple module

39.3. Making an HTTP request

39.4. Putting it all together

Summary

Lesson 40. Working with JSON data by using Aeson

40.1. Getting set up

40.1.1. Setting up stack

40.2. Using the Aeson library

40.3. Making your data types instances of FromJSON and ToJSON

40.3.1. The easy way

40.3.2. Writing your own instances of FromJSON and ToJSON

40.4. Putting it all together: reading your NOAA data

Summary

Lesson 41. Using databases in Haskell

41.1. Setting up your project

41.2. Using SQLite and setting up your database

41.2.1. Your Haskell data

41.3. Creating data—inserting users and checking out tools

41.3.1. Adding new users to your database

41.3.2. Creating checkouts

41.4. Reading data from the database and FromRow

41.4.1. Making your data an instance of FromRow

41.4.2. Listing users and tools

41.5. Updating existing data

41.6. Deleting data from your database

41.7. Putting it all together

Summary

Lesson 42. Efficient, stateful arrays in Haskell

42.1. Creating efficient arrays in Haskell with the UArray type

42.1.1. The inefficiencies of lazy lists

42.1.2. Creating a UArray

42.1.3. Updating your UArray

42.2. Mutating state with STUArray

42.3. Taking values out of the ST context

42.4. Implementing a bubble sort

Summary

 Afterword. What’s next?

A deeper dive into Haskell

More powerful type systems than Haskell?

Idris—programming with dependent types

Liquid Haskell—provable types

Other functional programming languages

Recommended programming languages in the Lisp family

Recommended programming languages in the ML family

Answers to end-of-lesson exercises

Unit 1

Lesson 2

Lesson 3

Lesson 4

Lesson 5

Lesson 6

Lesson 7

Lesson 8

Lesson 9

Unit 2

Lesson 11

Lesson 12

Lesson 13

Lesson 14

Unit 3

Lesson 16

Lesson 17

Lesson 18

Lesson 19

Unit 4

Lesson 21

Lesson 22

Lesson 23

Lesson 24

Lesson 25

Unit 5

Lesson 27

Lesson 28

Lesson 29

Lesson30

Lesson 31

Lesson 32

Unit 6

Unit 7

Lesson 38

Lesson 39

Lesson 40

Lesson 41

Lesson 42

Index

List of Figures

List of Tables

List of Listings

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.46.44