{-
-- EPITECH PROJECT, 2023
-- GLaDOS
-- File description:
-- Stack
-}

module Stack(top,
             push,
             pop,
             seek,
             size,
             clear) where

-- | Return the top of the stack
top :: [a] -> Maybe a
top :: forall a. [a] -> Maybe a
top [] = Maybe a
forall a. Maybe a
Nothing
top (a
x:[a]
_) = a -> Maybe a
forall a. a -> Maybe a
Just a
x

-- | Push a value onto the stack, return the new stack
push :: [a] -> a -> [a]
push :: forall a. [a] -> a -> [a]
push [] a
el = [a
el]
push [a]
l a
el = a
ela -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
l

-- | Remove the top of the stack, return a tuple containing
-- the old top and the new stack
pop :: [a] -> (Maybe a, [a])
pop :: forall a. [a] -> (Maybe a, [a])
pop [] = (Maybe a
forall a. Maybe a
Nothing, [])
pop (a
x:[a]
l) = (a -> Maybe a
forall a. a -> Maybe a
Just a
x, [a]
l)

-- | Return the first element of the stack validating the
-- function given as parameter
seek :: (a -> Bool) -> [a] -> Maybe a
seek :: forall a. (a -> Bool) -> [a] -> Maybe a
seek a -> Bool
_ [] = Maybe a
forall a. Maybe a
Nothing
seek a -> Bool
f (a
x:[a]
l) | a -> Bool
f a
x = a -> Maybe a
forall a. a -> Maybe a
Just a
x
             | Bool
otherwise = (a -> Bool) -> [a] -> Maybe a
forall a. (a -> Bool) -> [a] -> Maybe a
seek a -> Bool
f [a]
l

-- | Return the size of the stack
size :: [a] -> Int
size :: forall a. [a] -> Int
size [] = Int
0
size (a
_:[a]
xs) = [a] -> Int
forall a. [a] -> Int
size [a]
xs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1

-- | Clear the stack by return an empty stack
clear :: [a] -> [a]
clear :: forall a. [a] -> [a]
clear [a]
_ = []