Rule 110d-pat-cons
Patterns
(110)
TeX source
\defnrule[r:d-pat-cons]
{v=v_1\mathbin{\kw{::}}v_2\\
\jdgv{E}{v_1}{\pat_1}{\VE_1/\FAIL}\premsep
\jdgv{E}{v_2}{\pat_2}{\VE_2/\FAIL}}
{\jdgv{E}{v}{\pat_1\ \kw{::}\ \pat_2}{\VE_1/\FAIL+\VE_2/\FAIL}}Tests (25)
- elm / CaseAsPatternTestAn as-pattern in a case branch, `((x :: _) as whole)`, binds both the list head and the whole list.
- elm / CaseFanOutShadowTestA function that destructures a custom type in its parameter and then cases on a `Result` combines the bound field with the `Ok` value, or returns -1 for `Err`.
- 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 / CaseListThreeWayTestChecks that a `case` on a `List` with `[]`, `[x]` and `x :: xs` patterns, in that order, sends a one-element list to `[x]` rather than to the cons pattern.
- 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 / DeepNestStressTestDeeply nested lists, tuples, records and `let` expressions can be built and taken apart.
- 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 / FunctionAccumulatorFunctionLookupTestLooking up a function in a list of key and function pairs, and applying the function found, gives the right result.
- elm / HigherOrderFilterLikeFunctionTestA let-bound function that takes a predicate and a list, and keeps the list's head when the predicate holds, gives `[1]` for a predicate that always returns `True`.
- elm / LargeDispatchCaseTestChecks a `case` on a list of custom-type values whose branches contain a nested `case` on a String.
- elm / LetNumberListConsHeadTestThe head of a list of `number` literals, matched by a `::` pattern in a `case` and multiplied by a `Float`, is a `Float`.
- elm / LetNumberListFixedTestBoth elements of a list of `number` literals, matched by the fixed-length list pattern `[ a, b ]` and multiplied by a `Float`, are `Float`s.
- elm / MinimalConsPatternTestA list construction pattern matches a non-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 / StandardCoreEvaluationExamplesTestSmall programs for rules of the dynamic semantics of the Core that other tests reach only in passing, and examples from the Comments of A Standard Definition of Elm?, Chapter 6.
- 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 / TailRecCaseMultiBranchTypesTestA self tail-recursive search whose outer `case` has one branch per constructor of a custom type at the head of a list, with a nested `case` on a `String` inside one branch, finds the right element.
- 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 / TailRecMultiCaseWhileTestA tail-recursive fold over a list of custom-type values, with a `case` on string literals nested inside one list pattern and tail calls in every branch, accumulates the right pair of totals.
- 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-pat-cons] 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.