liveBook · Manning

Nothing

As explained in Learn Haskell by Example

Nothing is the Maybe constructor representing the absence of a contained value.

Nothing is the Maybe constructor for absence: it contains no value. A Maybe value is therefore either Just a value or Nothing, allowing a pure function to report failure without crashing. Nothing also represents an optional result or argument. section 3.3.2

Parsing an optional file path

A parser can return Just filePath when it receives exactly one path, and Nothing for every other argument list. Its result type becomes Maybe FilePath. Pattern matching handles both cases; fromMaybe supplies a default when the result is Nothing. section 3.3.2

Nothing carries meaning through pattern matching:

  • Use Nothing to encode a missing value or recoverable fault as data. section 3.3.2
  • Match Nothing separately because it contains no value to extract. section 3.3.2
  • In alter, Nothing can mean either that a key is absent or that an existing mapping should be deleted; Just supplies an inserted or updated value. section 5.2
Listing 3.5 A simple argument-parsing function with Maybe
parseArguments :: [String] -> Maybe FilePath
parseArguments [filePath] = Just filePath  #1
parseArguments _ = Nothing  #2

Browse a complete argument parser that returns Nothing when its input does not contain exactly one file path. section 3.3.2

How Learn Haskell by Example covers it

How deep it goes
Defines the constructor and demonstrates pattern matching, defaults, and associative-list updates without covering deeper Maybe abstractions.
Examples
Uses argument parsing, fromMaybe defaults, and an alter function where Nothing removes or signifies a missing mapping.
What it assumes
Assumes basic Haskell functions and pattern matching while introducing Maybe constructors and algebraic data types.
What you can do after
Apply Nothing through pattern matching to represent absence, choose defaults, and control pure updates.
How it connects
Connects Maybe, Just, pattern matching, algebraic data types, higher-order functions, and immutable data structures.

Also explained in

Compare how 2 books explain Nothing →

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.