Say a list has a length of three and I want to access the second, or middle element. What is the best way to do this?
Asked
Active
Viewed 252 times
1 Answers
1
You can use the list index operator !!
for this, which is defined as follows:
(!!)
: [a] -> Int -> a
List index (subscript) operator, starting from 0.
Here's a snipplet demonstrating its usage:
first : [Int] -> Int
first x =
let f = x!!0
in f
testFirst = scenario do
assert(first [3, 2, 1] == 3)

GeorgS
- 741
- 5
- 12