liveBook · Manning

Polymorphic type

As explained in Learn Haskell by Example

A polymorphic type is a type expression containing type variables so that one definition can apply to values of multiple concrete types.

A polymorphic type contains a type variable, allowing one definition to work with values of multiple concrete types. Graph nodes do not need a fixed element type, so the graph representation remains flexible. section 5.1.1

A polymorphic graph

An undirected, unweighted graph can use an adjacency list: type Graph a = [(a,a)]. The variable a represents the node type, while each tuple records a connection between two nodes. This suits a word-ladder graph, where each edge represents one step. section 5.1.1

The simple representation has practical limits: checking edges, gathering neighboring nodes, and inserting edges may require scanning the entire list. An adjacency map can provide faster indexing, represented here as DiGraph a = [(a,[a])]. section 5.1.1

How Learn Haskell by Example covers it

How deep it goes
Defines polymorphic graph aliases and contrasts their adjacency-list and adjacency-map performance trade-offs.
Examples
Uses a word-ladder graph, [(a,a)] adjacency lists, and [(a,[a])] adjacency maps.
What it assumes
Assumes basic graph terminology and familiarity with associative lists as a data-structure representation.
What you can do after
Apply it via polymorphic type aliases when representing graphs whose node type should remain flexible.
How it connects
Connects type variables with graphs, adjacency lists, associative lists, adjacency maps, and directed graphs.

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.