I've been banging my head over the wall for a few hours as I can't figure out how to write DFS in Haskell..
My graph is implemented as an adjacency list where the keys (or node name of the graph) are list indices:
0 -> 1
1 -> 0, 2
2 -> 1
As a Haskell list: [[1],[0,2],[1]]
Here's my code so far for DFS:
dfs graph visited node = helper graph visited (graph !! node) node
where helper _ _ visited [] _ = visited
helper graph visited (x:xs) currNode
| elem x visited = helper graph visited xs currNode
| otherwise = dfs graph (currNode:visited) x
I understand that the problem is that it doesn't backtrack and try another adjacent node once it reaches one end of the graph. I've been trying to modify the otherwise section of the guard in an attempt to fix it but can't seem to come up with something that works. What can I do to fix this?