liveBook · Manning

Sequence

As explained in Learn Haskell by Example

Sequence is a book-defined composition structure that represents musical elements arranged in succession.

A musical composition can arrange notes and pauses in succession. A melody is a sequence of notes; sequences can also contain other sequences or groups, such as chords played one after another. This requires a recursive structure rather than a flat collection. section 11.1

Figure 11.1 A melody is a sequence of notes
figure

A melody illustrates the simplest case: notes ordered to play one after another. section 11.1

In Haskell, `Sequence [NoteStructure a]` represents ordered composition elements alongside `Note`, `Pause`, and `Group`. Keeping them under `NoteStructure` lets composition operators combine the same overall kind of value, while the type parameter still makes ordinary structures homogeneous. section 11.1

A sequencing operator can hide both the `Sequence` constructor and its backing list. When either operand is already a sequence, it adds the other element or joins the two sequences; otherwise, it creates a new two-element sequence. This flattening avoids unnecessary nested sequences. section 11.3.2

Listing 11.9 An infix operator to construct sequences of compositions
(<~>) :: NoteStructure -> NoteStructure -> NoteStructure
(<~>) (Sequence xs) (Sequence ys) = Sequence $ xs ++ ys  #1
(<~>) (Sequence xs) x = Sequence $ xs ++ [x]     #2
(<~>) x (Sequence xs) = Sequence $ x : xs        #2
(<~>) a b = Sequence [a, b]  #3

infixr 3 <~>

The `<~>` operator merges existing sequences or constructs one from two composition elements, then assigns right-associative precedence. section 11.3.2

Sequencing three notes

`c 4 .| wn <~> e 4 .| wn <~> g 4 .| wn` produces one `Sequence` containing the C, E, and G notes in order. The same DSL also has `<:>` for groups, with `<~>` given higher precedence. section 11.3.2

Sequential data also appears as `Seq` from `Data.Sequence` when output must grow at its end. For a `WriterT` log, appending to a plain list repeatedly can cause quadratic work; `Seq` supports constant-time end appends and avoids that growth problem. section 15.1.2

How Learn Haskell by Example covers it

How deep it goes
Defines recursive sequence nesting and works through flattening operator cases, while leaving heterogeneous pitch representation as a problem.
Examples
Uses melodies, consecutive chords, a three-note C–E–G phrase, and WriterT output collection.
What it assumes
Assumes familiarity with previously defined Pitchable types while introducing recursive composition structures and custom operators.
What you can do after
Build it from scratch with a recursive composition type and a flattening infix sequencing operator.
How it connects
Connects sequences with NoteStructure, notes, pauses, Group, Pitchable, lists, and operator precedence.

Also explained in

Compare how 22 books explain sequence →

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.