Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does suspension points in haskell should work with an extra "space"?

When defining lists, we use suspension points without extra spaces like this:

Prelude> [3..5]
[3,4,5]
Prelude> [3 .. 5]
[3,4,5]

But used with enumerations seems extra spaces are required:

Prelude> [LT..GT]

<interactive>:2:2: Not in scope: ‘LT..’

<interactive>:2:2:
    A section must be enclosed in parentheses thus: (LT.. GT)
Prelude> [LT .. GT]
[LT,EQ,GT]

So question is: is it a syntax rule in Haskell? Or related to implementations?

like image 549
vik santata Avatar asked Mar 14 '23 06:03

vik santata


1 Answers

LT is a valid module name, and therefore you refer to the function (.) in that module name (or alias), not the syntactic sugar for enumerations. Since you didn't import LT (the module, not the data constructor), all of it's hypothetical functions are out of scope.

Here's a bogus example that does not lead to an out of scope error:

Prelude> import Prelude hiding (LT)
Prelude> import qualified Prelude as LT
Prelude LT> [LT..GT]

<interactive>:3:2:
    A section must be enclosed in parentheses thus: (LT.. GT)

Even with the parentheses it wouldn't type check. The section error can also be achieved with a single list:

Prelude> [+ 1]
<interactive>:1:2:
    A section must be enclosed in parentheses thus: (+ 1)

Prelude> :t [(+ 1)]
[(+ 1)] :: Num a => [a -> a]

TL;DR: Unless you want to specify something from another module, make sure have some space between . and a valid module name.

like image 128
Zeta Avatar answered Apr 26 '23 13:04

Zeta