verify graph path search

This commit is contained in:
hi 2025-08-15 10:10:20 +00:00
parent 687b5f1040
commit fa1a39d1da

View file

@ -35,3 +35,21 @@ Just ["succ","succ","succ","succ","succ","succ","succ","succ","succ","succ"]
ghci> bfs 13 0 (\x -> [("double", x+x), ("add", x+1)]) ghci> bfs 13 0 (\x -> [("double", x+x), ("add", x+1)])
Just ["add","double","add","double","double","add"] Just ["add","double","add","double","double","add"]
-} -}
data VerifyPathError vertex edge
= NoSuchEdge vertex edge
verifyPath
:: (Eq vertex, Ord vertex, Eq edge)
=> GetEdges vertex edge
-> vertex
-> vertex
-> [edge]
-> Either (VerifyPathError vertex edge) Bool
verifyPath getEdges goal start path =
case path of
[] -> Right $ start == goal
(thisEdge:nextEdges) ->
case map fst $ filter (\(next, realEdge) -> realEdge == thisEdge) $ getEdges start of
[] -> Left $ NoSuchEdge start thisEdge
(next:_) -> verifyPath getEdges goal next nextEdges