It’s pretty easy to represent a tree in haskell:
data Tree a = Node Tree a Tree | Leaf a
but that’s because it has no need for the concept of an imperative style “pointer” because each Node/Leaf has one, and only one parent. I guess I could represent it as a list of lists of Maybe Ints …to create a table with Nothing for those nodes without a path between and Just n for those that do… but that seems really ugly and unwieldy.
Disclaimer: below is a mostly pointless exercise in “tying the knot” technique. Fgl is the way to go if you want to actually use your graphs. However if you are wondering how it’s possible to represent cyclic data structures functionally, read on.
It is pretty easy to represent a graph in Haskell!
This is a cyclic, finite, recursive, purely functional data structure. Not a very efficient or beautiful one, but look, ma, no pointers! Here’s an exercise: include incoming edges in the vertex
It’s easy to build a full graph that has two (indistinguishable) copies of each edge:
but try to build one that has one copy of each! (Imagine that there’s some expensive computation involved in
e v1 v2).