Last active
August 5, 2021 16:03
-
-
Save appositum/5ac8873a7416b01ccca2dec346349134 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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