module Stack(top,
push,
pop,
seek,
size,
clear) where
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] -> 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
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)
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
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 :: [a] -> [a]
clear :: forall a. [a] -> [a]
clear [a]
_ = []