liveBook · Manning

Declarative programming

As explained in Learn Haskell by Example

Declarative programming is a programming paradigm that specifies what results or relationships are desired rather than prescribing an explicit sequence of operations.

Declarative programming describes a computation through definitions of results and intermediate values instead of prescribing an explicit sequence of instructions. Functional recipes produce values once, so later steps depend on available definitions rather than changing state. section 1.2.2 section 1.5

In Haskell, this style supports reusable, composable code. Pure functions consume inputs and produce outputs without side effects, while monadic programming provides a way to control effects such as file, network, database, and time interactions. section 1.2.2 section 1.2.3

A declarative design typically:

  • Breaks a complex problem into smaller definitions. section 4.3.4
  • Lets definitions reference one another without specifying their evaluation order. section 4.3.4
  • Keeps pure computations easy to test by comparing inputs with outputs. section 1.2.3

Formatting numbered lines

A line-formatting function can define numbers and text, convert missing numbers to empty strings, find the maximum width, pad each number, and combine the results. The final expression assembles the output; no execution order is prescribed. section 4.3.4

Listing 4.10 A function for transforming numbered lines into human-readable strings
prettyNumberedLines :: PadMode -> NumberedLines -> [String]
prettyNumberedLines mode lineNums =
  let (numbers, text) = unzip lineNums                      #1
      numberStrings = map (maybe "" show) numbers          #2
      maxLength = maximum (map length numberStrings)      #3
      paddedNumbers = map (pad mode maxLength) numberStrings    #4
   in zipWith (\n l -> n ++ " " ++ l) paddedNumbers text   #5

Inspect the complete Haskell definition and follow how each intermediate result contributes to the final formatted lines. section 4.3.4

How Learn Haskell by Example covers it

How deep it goes
Explains definition-based computation, purity, effects, and order-independent intermediate results, while deferring deeper effect mechanisms.
Examples
Demonstrates declarative formatting through numbered lines, intermediate values, padding, and final string assembly.
What it assumes
Assumes basic functions and variables, while introducing immutability, purity, side effects, and definition-oriented decomposition.
What you can do after
Build it from scratch by decomposing a transformation into named intermediate definitions and a final expression.
How it connects
Connects declarative programming with functional programming, pure functions, side effects, monadic programming, and maintainable abstractions.

Also explained in

Compare how 14 books explain declarative programming →

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.