Skip to content

Instantly share code, notes, and snippets.

@appositum
Last active August 5, 2021 16:03
Show Gist options
  • Save appositum/5ac8873a7416b01ccca2dec346349134 to your computer and use it in GitHub Desktop.
Save appositum/5ac8873a7416b01ccca2dec346349134 to your computer and use it in GitHub Desktop.
import qualified Data.Map as M
data Vertex = Vertex String deriving Eq
instance Show Vertex where
show (Vertex s) = "Vertex " ++ s
type Graph = [(Vertex, [Vertex])]
graph :: Graph
graph =
[ (v1, [v2, v3])
, (v2, [v1])
, (v3, [v1, v4])
, (v4, [v3])
] where
v1 = Vertex "v1"
v2 = Vertex "v2"
v3 = Vertex "v3"
v4 = Vertex "v4"
isAdjacent :: Vertex -> Vertex -> Either String Bool
isAdjacent v w =
case lookup v graph of
Nothing -> Left $ "Vertex not found in graph: " ++ show v
Just adj -> Right $ w `elem` adj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment