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

How do I access elements in a list by index in DAML?

假设一个列表的长度为三,我想访问第二个或中间的元素。执行此操作的最佳方法是什么?

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

(!!)
    : [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)