I want to check from which nodes a node is reachable.
So I want to give a graph and node and return a list of all nodes where I can reach the graph from. (I know there is Data.Graph, but I want to try to understand it from scratch, and don't use it on purpose.
To build up the graph I got the following Data.
data Node = Node String [Node]
The String is just a representation of a name the node has.
Which is the representaion of the graph.
node1 = Node "node1" [node2,node3]
node2 = Node "node2" [node3]
node3 = Node "node3" [node4,node5]
node4 = Node "node4" [node1,node2]
node5 = Node "node5" [node4]
Also I got the (directly) reaching Nodes in:
canReachNext (Node x y) = y
Now my basic idea was this:
listForReachables:: Node n => n -> [n]
listForReachables x
| contains[x] (canReachNext x) == False = listForReachables (head(canReachNext (x)))
| contains [x] (canReachNext x)== True = [x]
| otherwise = []
This ends in an endless Loop if I run
listForReachables node1
I get the reason why it ends in a loop. Because in this example the head will always have an other list.
My problem and where I am stuck is, that im used to the OOP and in this case it feels like I need a list to remember which nodes I've checked, and which nodes I still have to check.