Haskell, range downto without step [duplicate]

天大地大妈咪最大 提交于 2020-01-03 10:44:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!