如何在DAML中按索引访问列表中的元素?

时间:2019-01-22 16:30:29

标签: daml

说一个列表的长度为三,我想访问第二个或中间的元素。最好的方法是什么?

1 个答案:

答案 0 :(得分:1)

您可以为此使用列表索引运算符!!,其定义如下:

(!!)
    : [a] -> Int -> a

List index (subscript) operator, starting from 0.

下面是一个片段,演示了其用法:

first : [Int] -> Int
first x =
  let f = x!!0
  in f

testFirst = scenario do
  assert(first [3, 2, 1] == 3)