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

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

-- | Replicate the elements of a list a given number of times.
repli :: [a] -> Int -> [a]
repli :: forall a. [a] -> Int -> [a]
repli [a]
_ Int
0      = []
repli [] Int
_     = []
repli (a
x:[a]
xs) Int
n = a -> Int -> [a] -> [a]
forall a. a -> Int -> [a] -> [a]
prepend a
x Int
n ([a] -> Int -> [a]
forall a. [a] -> Int -> [a]
repli [a]
xs Int
n)

prepend :: a -> Int -> [a] -> [a]
prepend :: forall a. a -> Int -> [a] -> [a]
prepend a
_ Int
0 [a]
l = [a]
l
prepend a
x Int
n [a]
l
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = a -> Int -> [a] -> [a]
forall a. a -> Int -> [a] -> [a]
prepend a
x (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
l)
  | Bool
otherwise = [a]
forall a. HasCallStack => a
undefined