You are previewing Learn You a Haskell for Great Good!.

Learn You a Haskell for Great Good!

Cover of Learn You a Haskell for Great Good! by Miran Lipovaca Published by No Starch Press
  1. Learn You a Haskell for Great Good!
  2. Introduction
    1. So, What's Haskell?
    2. What You Need to Dive In
    3. Acknowledgments
  3. 1. Starting Out
    1. Calling Functions
    2. Baby's First Functions
    3. An Intro to Lists
      1. Concatenation
      2. Accessing List Elements
      3. Lists Inside Lists
      4. Comparing Lists
      5. More List Operations
    4. Texas Ranges
    5. I'm a List Comprehension
    6. Tuples
      1. Using Tuples
      2. Using Pairs
      3. Finding the Right Triangle
  4. 2. Believe the Type
    1. Explicit Type Declaration
    2. Common Haskell Types
    3. Type Variables
    4. Type Classes 101
      1. The Eq Type Class
      2. The Ord Type Class
      3. The Show Type Class
      4. The Read Type Class
      5. The Enum Type Class
      6. The Bounded Type Class
      7. The Num Type Class
      8. The Floating Type Class
      9. The Integral Type Class
      10. Some Final Notes on Type Classes
  5. 3. Syntax in Functions
    1. Pattern Matching
      1. Pattern Matching with Tuples
      2. Pattern Matching with Lists and List Comprehensions
      3. As-patterns
    2. Guards, Guards!
    3. where?!
      1. where's Scope
      2. Pattern Matching with where
      3. Functions in where Blocks
    4. let It Be
      1. let in List Comprehensions
      2. let in GHCi
    5. case Expressions
  6. 4. Hello Recursion!
    1. Maximum Awesome
    2. A Few More Recursive Functions
      1. replicate
      2. take
      3. reverse
      4. repeat
      5. zip
      6. elem
    3. Quick, Sort!
      1. The Algorithm
      2. The Code
    4. Thinking Recursively
  7. 5. Higher-Order Functions
    1. Curried Functions
      1. Sections
      2. Printing Functions
    2. Some Higher-Orderism Is in Order
      1. Implementing zipWith
      2. Implementing flip
    3. The Functional Programmer's Toolbox
      1. The map Function
      2. The filter Function
      3. More Examples of map and filter
      4. Mapping Functions with Multiple Parameters
    4. Lambdas
    5. I Fold You So
      1. Left Folds with foldl
      2. Right Folds with foldr
      3. The foldl and foldr1 Functions
      4. Some Fold Examples
      5. Another Way to Look at Folds
      6. Folding Infinite Lists
      7. Scans
    6. Function Application with $
    7. Function Composition
      1. Function Composition with Multiple Parameters
      2. Point-Free Style
  8. 6. Modules
    1. Importing Modules
    2. Solving Problems with Module Functions
      1. Counting Words
      2. Needle in the Haystack
      3. Caesar Cipher Salad
      4. On Strict Left Folds
      5. Let's Find Some Cool Numbers
    3. Mapping Keys to Values
      1. Almost As Good: Association Lists
      2. Enter Data.Map
    4. Making Our Own Modules
      1. A Geometry Module
      2. Hierarchical Modules
  9. 7. Making Our Own Types and Type Classes
    1. Defining a New Data Type
    2. Shaping Up
      1. Improving Shape with the Point Data Type
      2. Exporting Our Shapes in a Module
    3. Record Syntax
    4. Type Parameters
      1. Should We Parameterize Our Car?
      2. Vector von Doom
    5. Derived Instances
      1. Equating People
      2. Show Me How to Read
      3. Order in the Court!
      4. Any Day of the Week
    6. Type Synonyms
      1. Making Our Phonebook Prettier
      2. Parameterizing Type Synonyms
      3. Go Left, Then Right
    7. Recursive Data Structures
      1. Improving Our List
      2. Let's Plant a Tree
    8. Type Classes 102
      1. Inside the Eq Type Class
      2. A Traffic Light Data Type
      3. Subclassing
      4. Parameterized Types As Instances of Type Classes
    9. A Yes-No Type Class
    10. The Functor Type Class
      1. Maybe As a Functor
      2. Trees Are Functors, Too
      3. Either a As a Functor
    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
      1. Using let Inside I/O Actions
      2. Putting It in Reverse
    4. Some Useful I/O Functions
      1. putStr
      2. putChar
      3. print
      4. when
      5. sequence
      6. mapM
      7. forever
      8. forM
    5. I/O Action Review
  11. 9. More Input and More Output
    1. Files and Streams
      1. Input Redirection
      2. Getting Strings from Input Streams
      3. Transforming Input
    2. Reading and Writing Files
      1. Using the withFile Function
      2. It's Bracket Time
      3. Grab the Handles!
    3. To-Do Lists
      1. Deleting Items
      2. Cleaning Up
    4. Command-Line Arguments
    5. More Fun with To-Do Lists
      1. A Multitasking Task List
      2. Dealing with Bad Input
    6. Randomness
      1. Tossing a Coin
      2. More Random Functions
      3. Randomness and I/O
    7. Bytestrings
      1. Strict and Lazy Bytestrings
      2. Copying Files with Bytestrings
  12. 10. Functionally Solving Problems
    1. Reverse Polish Notation Calculator
      1. Calculating RPN Expressions
      2. Writing an RPN Function
      3. Adding More Operators
    2. Heathrow to London
      1. Calculating the Quickest Path
      2. Representing the Road System in Haskell
      3. Writing the Optimal Path Function
      4. Getting a Road System from the Input
  13. 11. Applicative Functors
    1. Functors Redux
      1. I/O Actions As Functors
      2. Functions As Functors
    2. Functor Laws
      1. Law 1
      2. Law 2
      3. Breaking the Law
    3. Using Applicative Functors
      1. Say Hello to Applicative
      2. Maybe the Applicative Functor
      3. The Applicative Style
      4. Lists
      5. IO Is An Applicative Functor, Too
      6. Functions As Applicatives
      7. Zip Lists
      8. Applicative Laws
    4. Useful Functions for Applicatives
  14. 12. Monoids
    1. Wrapping an Existing Type into a New Type
      1. Using newtype to Make Type Class Instances
      2. On newtype Laziness
      3. type vs. newtype vs. data
    2. About Those Monoids
      1. The Monoid Type Class
      2. The Monoid Laws
    3. Meet Some Monoids
      1. Lists Are Monoids
      2. Product and Sum
      3. Any and All
      4. The Ordering Monoid
      5. Maybe the Monoid
    4. Folding with Monoids
  15. 13. A Fistful of Monads
    1. Upgrading Our Applicative Functors
    2. Getting Your Feet Wet with Maybe
    3. The Monad Type Class
    4. Walk the Line
      1. Code, Code, Code
      2. I'll Fly Away
      3. Banana on a Wire
    5. do Notation
      1. Do As I Do
      2. Pierre Returns
      3. Pattern Matching and Failure
    6. The List Monad
      1. do Notation and List Comprehensions
      2. MonadPlus and the guard Function
      3. A Knight's Quest
    7. Monad Laws
      1. Left Identity
      2. Right Identity
      3. Associativity
  16. 14. For a Few Monads More
    1. Writer? I Hardly Knew Her!
      1. Monoids to the Rescue
      2. The Writer Type
      3. Using do Notation with Writer
      4. Adding Logging to Programs
      5. Inefficient List Construction
      6. Using Difference Lists
      7. Comparing Performance
    2. Reader? Ugh, Not This Joke Again
      1. Functions As Monads
      2. The Reader Monad
    3. Tasteful Stateful Computations
      1. Stateful Computations
      2. Stacks and Stones
      3. The State Monad
      4. Getting and Setting State
      5. Randomness and the State Monad
    4. Error Error on the Wall
    5. Some Useful Monadic Functions
      1. liftM and Friends
      2. The join Function
      3. filterM
      4. foldM
    6. Making a Safe RPN Calculator
    7. Composing Monadic Functions
    8. Making Monads
  17. 15. Zippers
    1. Taking a Walk
      1. A Trail of Breadcrumbs
      2. Going Back Up
      3. Manipulating Trees Under Focus
      4. Going Straight to the Top, Where the Air Is Fresh and Clean!
    2. Focusing on Lists
    3. A Very Simple Filesystem
      1. Making a Zipper for Our Filesystem
      2. Manipulating a Filesystem
    4. Watch Your Step
    5. Thanks for Reading!
  18. Index
  19. About the Author
  20. Colophon
  21. A. Updates
  22. Copyright
O'Reilly logo

Chapter 5. Higher-Order Functions

Haskell functions can take functions as parameters and return functions as return values. A function that does either of these things is called a higher-order function. Higher-order functions are a really powerful way of solving problems and thinking about programs, and they’re indispensable when using a functional programming language like Haskell.

Curried Functions

Every function in Haskell officially takes only one parameter. But we have defined and used several functions that take more than one parameter so far—how is that possible?

image with no caption

Well, it’s a clever trick! All the functions we’ve used so far that accepted multiple ...

The best content for your career. Discover unlimited learning on demand for around $1/day.