{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}

{- |
Description: Remove element from a list
Copyright: Copyright (C) 2021 Yoo Chung
License: GPL-3.0-or-later
Maintainer: dev@chungyc.org

Some solutions to "Problems.P20" of Ninety-Nine Haskell "Problems".
-}
module Solutions.P20 (removeAt) where

-- | Remove the @k@th element from a list.
-- Return the element removed and the residue list.
removeAt :: Int -> [a] -> (a,[a])
removeAt :: forall a. Int -> [a] -> (a, [a])
removeAt Int
n [a]
xs = (a
e, [a]
front [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
back)
  where front :: [a]
front = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) [a]
xs
        (a
e : [a]
back) = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) [a]
xs