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

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

-- | Reverse a list.
myReverse :: [a] -> [a]
myReverse :: forall a. [a] -> [a]
myReverse [a]
l = [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
accumulate [a]
l []

-- | Accumulates into the partial reversed list in the second argument
-- as extracted from the remainder of the original list in the first argument.
--
-- Returns the fully accumulated reversed list.
accumulate :: [a] -> [a] -> [a]
accumulate :: forall a. [a] -> [a] -> [a]
accumulate [] [a]
xs     = [a]
xs
accumulate (a
x:[a]
xs) [a]
ys = [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
accumulate [a]
xs (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
ys)