Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell, range downto without step [duplicate]

Tags:

range

haskell

Why in Haskell is not working range downto without step

[7..1] => []

but working only this

[7,6..1] => [7,6,5,4,3,2,1] 
like image 355
502 Avatar asked Aug 07 '11 11:08

502


3 Answers

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

There might be situations where you want a range [x..y] where y < x and where you expect the range to be empty. This would create subtle bugs if Haskell would simply step downwards in these cases.

like image 176
VVS Avatar answered Oct 15 '22 06:10

VVS


3.10. Arithmetic sequences

[...] Arithmetic sequences satisfy these identities:

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

6.3.4 The Enum Class

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

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

From Haskell 2010 Language Report.

like image 33
Cat Plus Plus Avatar answered Oct 15 '22 05:10

Cat Plus Plus


Without an indication of the step, haskell assumes it to be +1 and returns an empty list if it's not applicable to the given parameters.

Any increment apart from +1 has to be explicitly suggested; not only positive integrers > 1.

like image 32
cbrandolino Avatar answered Oct 15 '22 06:10

cbrandolino