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

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

-- | Drop every @n@th element from a list.
dropEvery :: [a] -> Int -> [a]
dropEvery :: forall a. [a] -> Int -> [a]
dropEvery [a]
l Int
n = [a] -> Int -> [a]
forall a. [a] -> Int -> [a]
step [a]
l Int
1
  where step :: [a] -> Int -> [a]
step [] Int
_ = []
        step (a
x:[a]
xs) Int
k
          | Int
k Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = [a] -> Int -> [a]
step [a]
xs (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
          | Bool
otherwise      = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> Int -> [a]
step [a]
xs (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)