Rule 102d-atpat-nil
Atomic Patterns
(102)
TeX source
\defnrule[r:d-atpat-nil]
{v=\kw{[]}}
{\jdgv{E}{v}{\kw{[}\ \kw{]}}{\emptymap}}Tests (14)
- elm / CaseListConsTestChecks that a recursive function matching a list against `[]` and `x :: xs` sums the empty list, a one-element list and a three-element list correctly.
- elm / CaseListTestChecks that a `case` on a `List` distinguishes the empty list, a list of exactly one element (`[_]`) and any longer list (`_`).
- elm / CaseMixedPatternTestChecks that a single `case` can match a custom type constructor whose argument is itself matched as a list, with `x :: _` and `[]` patterns nested inside the constructor pattern.
- elm / EmbeddedNilCaptureTestA local function that refers to an empty list defined in the enclosing `let` matches it against the `[]` pattern.
- elm / EmbeddedNilRecursiveTestA recursive function that returns `[]` for an empty list builds the doubled positive elements of its input, skipping the rest.
- elm / EmbeddedNothingRecursiveTestA recursive list search returns `Just` the first matching element, or `Nothing` once it reaches the end of the list.
- elm / EmbeddedReturnNilTestA recursive filter returns the empty list when no element passes, and the caller receives it like any other list.
- elm / ErasedListSpecializationTestA polymorphic self-recursive `count : List a -> Int` gives the right length when called both on `List Int` values and on `[]`, whose element type nothing constrains.
- elm / MinimalNilPatternTestThe empty-list pattern matches the empty list.
- elm / RecursiveListLengthTestA self-recursive function that pattern matches on `[]` and `_ :: rest` counts the elements of lists of length 0, 3 and 5.
- elm / TailRecBoolCarryTestA self tail-recursive function that carries a `Bool` argument from one call to the next, and sets it from a branch inside a `case`, returns the right result.
- elm / TailRecCaseDestructTestSelf tail-recursive functions whose tail calls sit in a `case` branch that destructures a list with `x :: xs` give the right results.
- elm / TailRecFoldlCaseTestA hand-written left fold, defined as a self tail-recursive local function in `main`'s `let` and matching on the list with `case`, sums `[ 1, 2, 3 ]` to 6.
- elm / TailRecNestedCaseTestSelf-recursive list functions whose tail call sits in an `if` branch or behind a `case` with several list patterns return the right results.
Discussions
Discuss this rule·Search the discussions on GitHub
Discussions are on GitHub; one about this rule has [r:d-atpat-nil] in its title.
A Standard Definition of Elm? (Draft version 0.4) © Rupert Smith. The text is licensed under CC BY 4.0: share and adapt it freely, with credit and a link to the licence.