Difference between revisions of "Array indexing: improved safety with modular arithmetic"

From HaskellWiki
Jump to navigation Jump to search
(Created page with "Category:Proposals Instead of an error being triggered if an array access is outside its bounds: <pre> Prelude Data.Array> array (0, 1) (zip [0, 1] [False, True]) ! 2 *...")
 
(Blanked the page)
 
Line 1: Line 1:
[[Category:Proposals]]
 
 
 
Instead of an error being triggered if an array access is outside its bounds:
 
 
<pre>
 
Prelude Data.Array> array (0, 1) (zip [0, 1] [False, True]) ! 2
 
*** Exception: Ix{Integer}.index: Index (2) out of range ((0,1))
 
</pre>
 
 
use the given index modulo the array's length:
 
 
<pre>
 
Prelude Data.Array> array (0, 1) (zip [0, 1] [False, True]) ! 2
 
False
 
</pre>
 
 
As an added benefit, accessing the end elements of an array would be simplified:
 
 
<pre>
 
Prelude Data.Array> [ array (0, 25) (zip [0..25] ['a'..'z']) ! n | n <- [-1, -2 .. -8] ]
 
"zyxwvuts"
 
</pre>
 
 
[[User:Atravers|Atravers]] 04:54, 19 January 2019 (UTC)
 

Latest revision as of 06:55, 20 January 2019