|Version 1 (modified by 8 years ago) (diff),|
|nhc98|| full (|
|UHC||full (doesn't support n+k patterns at all)|
n+k patterns provide a concise, natural, and familiar notation for recursion over naturals. When used with non-negative types, such as
Word, they allow functions to be cleanly defined using non-overlapping patterns.
n+k patterns are a non-orthogonal feature. No other pattern has special notation like this, although there is a ViewPatterns extension which does something not entirely dissimilar in a general way.
+ symbol is being abused here; it doesn't really mean
+, and if
+ happens to be bound to something other than
Prelude.+ the notation's behaviour doesn't change. Counterintuitively, the desugaring does include references to a number of other
Working on all
Integral types is inconsistent with the
n >= 0 condition, which means that
n is really a natural number.
There has long been a general consensus in the community that
n+k patterns should be removed. Indeed, the report already admits "Many people feel that n+k patterns should not be used. These patterns may be removed or changed in future versions of Haskell." (Haskell 98 Revised Report, Section 3.17.2).
In Section 3.17.1 replace:
In Section 3.17.2 remove:
- Matching an
nis a variable and
kis a positive integer literal) against a value
x >= k, resulting in the binding of
x - k, and fails otherwise. Again, the functions
-are overloaded, depending on the type of the pattern. The match diverges if the comparison diverges.
The interpretation of the literal
k is the same as in numeric literal patterns, except that only integer literals are allowed.
In Section 3.17.2 remove:
n+kpattern can only be matched against a value in the class
Many people feel that
n+k patterns should not be used. These patterns may be removed or changed in future versions of Haskell.
In Section 3.17.3 remove:
In Section 220.127.116.11 remove:
A note about syntax.
It is usually straightforward to tell whether a binding is a pattern binding or a function binding, but the existence of
n+k patterns sometimes confuses the issue. Here are four examples:
x + 1 = ... -- Function binding, defines (+) -- Equivalent to (+) x 1 = ... (x + 1) = ... -- Pattern binding, defines x (x + 1) * y = ... -- Function binding, defines (*) -- Equivalent to (*) (x+1) y = ... (x + 1) y = ... -- Function binding, defines (+) -- Equivalent to (+) x 1 y = ...
The first two can be distinguished because a pattern binding has a pat0 on the left hand side, not a pat —- the former cannot be an unparenthesised