Skip to content

Instantly share code, notes, and snippets.

@pietroluongo
Last active June 27, 2019 22:41
Show Gist options
  • Save pietroluongo/885a6c3885ca8a49b6770704e54d2ca5 to your computer and use it in GitHub Desktop.
Save pietroluongo/885a6c3885ca8a49b6770704e54d2ca5 to your computer and use it in GitHub Desktop.
Lista de Haskell, ato II
-- Monads https://medium.com/@julianoalves/functors-applicatives-e-monads-explicados-com-desenhos-2c45d5db7d25
data Arv a = Vazia | Node a (Arv a) (Arv a) deriving (Eq, Show, Read)
instance Functor Arv where
fmap f Vazia = Vazia
fmap f (Node a l r) = Node (f a) (fmap f l) (fmap f r)
singleton :: a -> Arv a
singleton a = Node a Vazia Vazia
insere :: (Ord a) => a -> Arv a -> Arv a
insere x Vazia = singleton x
insere x (Node a l r)
| x == a = Node x l r
| x < a = Node a (insere x l) r
| x > a = Node a l (insere x r)
criar :: (Ord a) => [a] -> Arv a
criar xs = foldr insere Vazia xs
buscar :: (Ord a) => a -> Arv a -> Bool
buscar _ Vazia = False
buscar x (Node a l r)
| x == a = True
| x < a = buscar x l
| x > a = buscar x r
data Fila a = Empty | Fila a (Fila a) deriving (Show)
vazia :: Fila a
vazia = Empty
-- inserir :: (Maybe a) => a -> Fila a -> Fila a
insereInicio a Empty = Fila (a) (Empty)
insereInicio a (Fila x y) = Fila (a) (Fila x y)
insereFinal a Empty = Fila (Empty) (a)
insereFinal a (Fila x y) = Fila (Fila x y) a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment