liveBook · Manning

Const

As explained in Learn Haskell by Example

const is a standard Haskell function that returns its first argument while ignoring its second argument.

const returns its first argument and ignores its second. Its polymorphic type, a -> b -> a, allows the ignored value to have a different type. section 4.2.1

Applying only const's first argument creates a unary function. Thus, const True behaves like a function that accepts any value and returns True. section 4.2.1

Line-numbering predicates

Use numberLines (const True) (const True) to number every line. Replace the second constant with isNotEmpty to number only non-empty lines, or use isNotEmpty for both predicates to number and increment non-empty lines. section 4.2.1

const :: a -> b -> a
const x = (\_  -> x)

Compare const's polymorphic definition with its equivalent form that explicitly ignores the second argument. section 4.2.1

numberAllLines :: [String] -> NumberedLines
numberAllLines = numberLines (const True) (const True)

See const used through partial application to assemble a concise line-numbering function. section 4.2.1

How Learn Haskell by Example covers it

How deep it goes
Explains the definition, polymorphic type, partial application, and eta reduction through line-numbering examples.
Examples
Demonstrates const True in line numbering and contrasts constant and non-empty-line predicates.
What it assumes
Introduces partial application while assuming familiarity with functions, arguments, and type signatures.
What you can do after
Apply const to build constant unary functions and configure existing functions with partial application.
How it connects
Connects const with partial function application, eta reduction, predicates, and reusable functions.

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.