Why is it that when I do range in Haskell, this works:
[LT .. GT]
but this doesn\'t:
[LT..GT]
and what do
Because of the maximal munch rule, LT..
gets interpreted as the qualified name of the (.)
operator in the LT
module. Since you can define your own operators in Haskell, the language allows you to fully qualify the names of operators in the same way as you can with functions.
This leads to an ambiguity with the ..
used in ranges when the name of the operator starts with .
, which is resolved by using the maximal munch rule, which says that the longest match wins.
For example, Prelude..
is the qualified name of the function composition operator.
> :info Prelude..
(.) :: (b -> c) -> (a -> b) -> a -> c -- Defined in GHC.Base
infixr 9 .
> (+3) Prelude.. (*2) $ 42
87
The reason why [1..3]
or [x..y]
works, is because a module name must begin with an upper case letter, so 1..
and x..
cannot be qualified names.