问题
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]
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/6972599/haskell-range-downto-without-step