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

前端 未结 3 1250
猫巷女王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:20

    It's because LT.. is interpreted as the . operator in the LT module.


    :1:2:
        Failed to load interface for `LT':
          Use -v to see a list of the files searched for.
    

    It means GHC cannot find a module named LT. The same message appears if you use a qualified name with a non-existing library:

    Prelude> SDJKASD.sdfhj
    
    :1:1:
        Failed to load interface for `SDJKASD':
          Use -v to see a list of the files searched for.
    

    :1:2:
        A section must be enclosed in parentheses thus: (`LT..` GT)
    

    In Haskell, a section is an infix operator with a partial application, e.g. (* 3), which is equivalent to \x -> x * 3.

    In your case, LT.. is interpreted as an infix . operator, and the GT is part of the section formed with this operator.

    A section must be enclosed in parenthesis, and since the misinterpretation does not, the parser will complain like this.

    Another example of the error:

    Prelude> [* 3]
    
    :1:2:
        A section must be enclosed in parentheses thus: (* 3)
    

提交回复
热议问题