buildMerge :: (Ord a) => Int -> Int -> Merge a (a -> r) -> Merge a r
buildMerge n m = \case
  (Merge i j f g k)
    | i == n    -> take2
    | j == m    -> take1
    | f i < g j -> take1
    | otherwise -> take2
    where
      take1 = Merge (i + 1) j f g (k (f i))
      take2 = Merge i (j + 1) f g (k (g j))
buildMerge :: (Ord a) => Int -> Int -> Merge a (a -> r) -> Merge a r
buildMerge n m = \case
  (Merge i j f g k)
    | i == n    -> take2
    | j == m    -> take1
    | f i < g j -> take1
    | otherwise -> take2
    where
      take1 = Merge (i + 1) j f g (k (f i))
      take2 = Merge i (j + 1) f g (k (g j))
buildMerge n m = \case
  (Merge i j f g k)
    | i == n    -> take2
    | j == m    -> take1
    | f i < g j -> take1
    | otherwise -> take2
    where
      take1 = Merge (i + 1) j f g (k (f i))
      take2 = Merge i (j + 1) f g (k (g j))