Haskell, range down without a step

Why Haskell doesn't work range down without step

[7..1] => [] 

but only works with this

 [7,6..1] => [7,6,5,4,3,2,1] 
+6
source share
3 answers

3.10. Arithmetic sequences

[...] Arithmetic sequences satisfy these identities:

  • [...]
  • [e1..e3] = enumFromTo e1 e3
  • [...]

6.3.4 Class Enum

For Int and Integer types, enumeration functions have the following meanings:

  • [...]
  • The sequence enumFromTo e1 e3 is a list of [e1, e1 + 1, e1 + 2, ... e3]. The list is empty if e1> e3.
  • [...]

From the Haskell 2010 Language Report .

+8
source

Haskell has no way of knowing that you want to take step -1 until you give it a hint.

There may be situations when you need a range [x..y] where y < x and where you expect the range to be empty. This would create subtle errors if Haskell simply went down in these cases.

+13
source

Without specifying a step, haskell assumes it is +1 and returns an empty list if it is not applicable to the given parameters.

Any addition other than +1 should be explicitly suggested; not only positive integrals> 1.

+2
source

Source: https://habr.com/ru/post/894481/


All Articles