Of course, it is useful to have a list that has a pointer to a specific place in the list. However, the method that usually runs in Haskell is slightly different - instead of using an explicit pointer, we tend to use a zipper.
The list of lightning looks like this:
data ListZipper a = LZ [a] a [a] deriving (Show)
, a , , [a] , [a] .
, , [0, 1, 2, *3*, 4, 5, 6]
LZ [2,1,0] 3 [4,5,6]
,
left (LZ (a:as) b bs) = LZ as a (b:bs)
right (LZ as a (b:bs)) = LZ (a:as) b bs
n , , , n
times n f = (!!n) . iterate f
,
>> let lz = LZ [2,1,0] 3 [4,5,6]
>> (3 `times` left) lz
LZ [] 0 [1,2,3,4,5,6]
findIndex next
next :: ListZipper a -> (a, ListZipper a)
next = findIndex 1
findIndex :: Int -> ListZipper a -> (a, ListZipper a)
findIndex n x = let y@(LZ _ a _) = (n `times` right) x in (a, y)