Array indexing: improved safety with modular arithmetic
Instead of an error being triggered if an array access is outside its bounds:
Prelude Data.Array> array (0, 1) (zip [0, 1] [False, True]) ! 2 *** Exception: Ix{Integer}.index: Index (2) out of range ((0,1))
use the given index modulo the array's length:
Prelude Data.Array> array (0, 1) (zip [0, 1] [False, True]) ! 2 False
As an added benefit, accessing the end elements of an array would be simplified:
Prelude Data.Array> [ array (0, 25) (zip [0..25] ['a'..'z']) ! n | n <- [-1, -2 .. -8] ] "zyxwvuts"
Atravers 04:54, 19 January 2019 (UTC)