[-5..0] -> -5,-4,-3,-2,-1,0
but
[-10..-5] -> "not in scope ..-"
to get it to work
[-10.. -5]
or [-10 .. -5]
sup with dat
... which is a big reason why we hail satan, tbh
For the record, this is arising from the fact that -
is a unary operator and not part of the integer literal. This by itself makes a lot of sense, but combined with the flexibility of Haskell's infix identifier syntax, can produce some weirdness. The above is where you start seeing that weirdness.
The following might make it a bit more clear:
infixl 7 ..-
(..-) :: Int -> Int -> Int
a ..- b = [a .. -b]
Basically, Haskell is parsing ..-
as a single infix operator that you're applying to the results of the expressions -(10)
and 5
. You can fake your way around this using the definition I gave above, or you can insert spaces (as you did). Certainly not the clearest error message, but it's sort of hard for GHC to do better in this case due to the flexibility of its identifier lexing.
As a sidebar, note that the operator I gave can't be used within a list comprehension ([ ... ]
), so it only partially fakes around your issue.
As a deeper sidebar, it is technically possible for GHC to avoid some of these problems altogether by treating list comprehensions as a complex mixfix operator. However, as with nearly every parser in the history of ever, mixfix operators are a hack that don't work correctly in any sense of the word. In this particular case, we don't have a |
in our comprehension, so there's no way for Haskell to directly disambiguate between our intended comprehension and a single element list containing the results of the expression -10 ..- 5
. A more sophisticated generalized parser could return a forest containing both possibilities, allowing the typechecker to disambiguate (and ultimately select the option we originally intended). GHC's parser is too simplistic to perform this sort of late-stage disambiguation though, and so the designers had to (arbitrarily) choose one option or the other to get precedence, in this case the unary list with the funky operator.
[-10..(-5)] should work
Can you perhaps use spaces consistently? Then no chance of ambiguity.
this must be correct since everyone knows that all haskell code is perfect and infallible. God literally coded The Universe in Haskell. p sure that's in the bible.