forwardSearch2 :: IntArray -> Int -> Int -> Elem -> Elem -> IO Int
forwardSearch2 !vec !start !end !x1 !x2 = go start end False
  where
    next !i !e !b = let !j = i+1
                    in if j == e
                         then (if b then (-1,e,True) else (0,start,True))
                         else (j,e,b)

    go !i !e !b = do
        h <- M.readArray vec i
        if h == x1 || h == x2
          then return i
          else do
              let (!i',!e',!b') = next i e b
              if (i' < 0) then return (-1) else go i' e' b'



forwardSearch2 :: IntArray -> Int -> Int -> Elem -> Elem -> IO Int
forwardSearch2 !vec !start !end !x1 !x2 = go start end False
  where
    next !i !e !b = let !j = i+1
                    in if j == e
                         then (if b then (-1,e,True) else (0,start,True))
                         else (j,e,b)

    go !i !e !b = do
        h <- M.readArray vec i
        if h == x1 || h == x2
          then return i
          else do
              let (!i',!e',!b') = next i e b
              if (i' < 0) then return (-1) else go i' e' b'



forwardSearch2 !vec !start !end !x1 !x2 = go start end False
  where
    next !i !e !b = let !j = i+1
                    in if j == e
                         then (if b then (-1,e,True) else (0,start,True))
                         else (j,e,b)

    go !i !e !b = do
        h <- M.readArray vec i
        if h == x1 || h == x2
          then return i
          else do
              let (!i',!e',!b') = next i e b
              if (i' < 0) then return (-1) else go i' e' b'


