默认
[1..5]
给出这个
[1,2,3,4,5]
也可以使用范围功能完成.是否可以更改点之间的步长,以便我可以得到类似下面的内容?
[1,1.5,2,2.5,3,3.5,4,4.5,5]
Johannes Wei.. 21
[1,1.5..5]
Paul Johnson.. 18
你必须小心浮点运算.它不能精确地代表1.1,所以如果你试试
Prelude> [0,0.1 .. 1] [0.0,0.1,0.2,0.30000000000000004,0.4,0.5,0.6,0.7,0.7999999999999999,0.8999999999999999,0.9999999999999999]
最好的方式更像是:
Prelude> map (/10) [0..10] [0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
mattiast.. 6
实际上,[1..5]
是语法糖
enumFromTo 1 5
并[1,1.5..5]
为
enumFromThenTo 1 1.5 5
有关更多信息,请参阅http://en.wikibooks.org/wiki/Haskell/Syntactic_sugar
[1,1.5..5]
你必须小心浮点运算.它不能精确地代表1.1,所以如果你试试
Prelude> [0,0.1 .. 1] [0.0,0.1,0.2,0.30000000000000004,0.4,0.5,0.6,0.7,0.7999999999999999,0.8999999999999999,0.9999999999999999]
最好的方式更像是:
Prelude> map (/10) [0..10] [0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
实际上,[1..5]
是语法糖
enumFromTo 1 5
并[1,1.5..5]
为
enumFromThenTo 1 1.5 5
有关更多信息,请参阅http://en.wikibooks.org/wiki/Haskell/Syntactic_sugar