一个简单的解决方案是使用某种形式的间接,例如索引
type Vertex = Integer
data Graph = Graph [Vertex] [(Vertex, Vertex)]
infGraph = Graph [1..] [(a, b) | a <- [1..], b <- [1..]]
然而,这并不像打结那样令人满意
data Vertex = Vertex { tag :: Integer
, edges :: [Vertex] }
type Graph = [Vertex] -- A graph is determined by the set of vertices
-- Construct a graph of infinitely many vertices in which
-- each vertex is connected.
infGraph = map (flip Vertex infGraph) [1..]
infGraph' = map (\v' -> v' infGraph') . map Vertex $ [1..]
我们map
Vertex
over [1..]
which 给了我们一个函数列表,[Vertex] -> Vertex
这些函数需要一个边列表来连接每个顶点。由于infGraph
是所有顶点的列表,我们将其传递给每个顶点Vertex
并打结。
当然,对于认真的工作,请使用package。