module Foo where

foo :: (Int,Int) -> [Int] -> [Int]
{-# INLINE foo #-}
foo (i,n) xs = loop i (xs,i,n)
  where
    loop i s
      | i < n = case step s of
                  Just (s',x) -> x : loop (i+1) s'
                  Nothing     -> []
      | otherwise = case step s of
                      Just (s',x) -> x : loop (i-1) s'
                      Nothing     -> []

    {-# INLINE [0] step #-}
    step (xs,i,k)
      | i < k     = case xs of (y:ys) -> Just ((ys,i+1+1+1+1+1+1+1,k::Int),y)
      | otherwise = Nothing

