Skip to content

Instantly share code, notes, and snippets.

@lironsade
Created March 10, 2019 13:28
Show Gist options
  • Save lironsade/c363953af47426e59357b8af9d954264 to your computer and use it in GitHub Desktop.
Save lironsade/c363953af47426e59357b8af9d954264 to your computer and use it in GitHub Desktop.
data List a = Node a (List a) | Empty -- Recursive
data Maze = Maze {grid::Grid, visited::Path, stack::Stack}
type Path = List Cell
--type Stack = [Cell]
type Stack = List Cell
type Grid = [[Char]]
-- Linked List --
push :: List a -> List a -> List a
push Empty ys = ys
push (Node x xs) ys = Node x (push xs ys)
peek :: List a -> a
peek Empty = error "Can't peek"
peek (Node x xs) = x
pop :: List a -> List a
pop Empty = error "Can't pop"
pop (Node x xs) = xs
elem' :: (Eq a) => a -> List a -> Bool
elem' _ Empty = False
elem' y (Node x xs) = y == x || (elem' y xs)
instance Functor List where
-- fmap :: (a -> b) -> List a -> List b
fmap _ Empty = Empty
fmap f (Node x xs) = Node (f x) (fmap f xs)
instance Applicative List where
-- pure :: a -> List a
pure x = Node x Empty
-- (<*>) :: List (a -> b) -> List a -> List b
Empty <*> _ = Empty
(Node g gs) <*> xs = fmap g xs `push` (gs <*> xs)
instance Monad List where
-- (>>=) :: List a -> (a -> List b) -> List b
Empty >>= _ = Empty
(Node x xs) >>= f = (f x) `push` (xs >>= f)
instance Monoid (List a) where
mappend = push
mempty = Empty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment