liveBook · Manning

Partial application

As explained in Learn Haskell by Example

Partial function application is the evaluation of a function with fewer arguments than its full arity, producing a function that accepts the remaining arguments.

Partial application supplies only some arguments to a function. Instead of failing, the expression produces a new function that accepts the remaining arguments. For example, `const True` fixes `const`'s first argument and yields a unary function that ignores its input and returns `True`. section 4.2.1

Configure a general function

A line-numbering function can be specialized by fixing its condition arguments: `numberAllLines = numberLines (const True) (const True)`. The result remains a function waiting for the text input. Other fixed conditions create variants that skip numbering or incrementing empty lines. section 4.2.1 section 4.5

Use partial application to derive focused functions from a general implementation rather than repeating its logic. Argument order matters: placing a configuration argument first lets it be fixed early. A generalized `pad` function therefore takes `PadMode` first, allowing `padLeft = pad PadLeft` and `padRight = pad PadRight`. section 4.3.1 section 4.3.2

Listing 4.9 Functions for left and right padding based on a generalized padding function
padLeft :: Int -> String -> String
padLeft = pad PadLeft                #1

padRight :: Int -> String -> String
padRight = pad PadRight        #2

Fixing the padding mode leaves a reusable function that still accepts width and string arguments. section 4.3.2

Partial application also shortens function arguments passed to higher-order functions. `map (maybe "" show) numbers` fixes the first two arguments of `maybe`, leaving a function that converts each `Maybe Int` to a string. Eta reduction can remove an explicitly named parameter when a definition merely passes it on, making equivalent definitions more concise. section 4.3.4 section 4.2.1

How Learn Haskell by Example covers it

How deep it goes
Defines partial application, derives `const True` and its type, and relates concise forms to eta reduction.
Examples
Uses `const True`, line-numbering variants, `padLeft` and `padRight`, and `map (maybe "" show)` over `Maybe Int` values.
What it assumes
Builds from function type signatures, lambdas, and `const` while explaining how a supplied argument fixes a type variable.
What you can do after
Apply it to create specialized functions from general Haskell functions without duplicating implementations.
How it connects
Connects partial application with higher-order functions, eta reduction, function types, `Maybe`, `map`, and algebraic data types.

Also explained in

Compare how 10 books explain partial application →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.