Why Haskell range needs spaces when using [LT .. GT]?

前端 未结 3 1228
猫巷女王i
猫巷女王i 2021-01-07 22:01

Why is it that when I do range in Haskell, this works:

[LT .. GT]

but this doesn\'t:

[LT..GT]

and what do

3条回答
  •  再見小時候
    2021-01-07 22:30

    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.

提交回复
热议问题