title: DFS implementation in Haskell
body: <p>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..</p>
  
  <p>My graph is implemented as an adjacency list where the keys (or node name of the graph) are list indices:</p>
  
  <pre><code>0 -&gt; 1
  1 -&gt; 0, 2
  2 -&gt; 1
  
  As a Haskell list: [[1],[0,2],[1]]
  </code></pre>
  
  <p>Here's my code so far for DFS:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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?</p>