mgo*_*old 34
Elm在0.12.1中添加了数组.
import Array
myArray = Array.fromList [1..5]
myItem = Array.get 2 myArray
Run Code Online (Sandbox Code Playgroud)
数组是零索引的.目前不支持负指数(我知道这很糟糕).
请注意myItem : Maybe Int.Elm尽其所能避免运行时错误,因此out of bounds access返回显式Nothing.
如果你发现自己想要索引列表而不是头部和尾部,你应该考虑使用数组.
Dan*_*res 14
在榆树中没有相同的东西.你当然可以自己实现它.
(注意:这不是"总"函数,因此当索引超出范围时会创建异常).
infixl 9 !!
(!!) : [a] -> Int -> a
xs !! n = head (drop n xs)
Run Code Online (Sandbox Code Playgroud)
更好的方法是使用Maybe数据类型定义一个总函数.
infixl 9 !!
(!!) : [a] -> Int -> Maybe a
xs !! n =
if | n < 0 -> Nothing
| otherwise -> case (xs,n) of
([],_) -> Nothing
(x::xs,0) -> Just x
(_::xs,n) -> xs !! (n-1)
Run Code Online (Sandbox Code Playgroud)