Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

Can you represent a closed network at compile time in F#?

I'm wanting to represent a directed network graph that is closed, e.g. where the edges would look like:

N1->N2
N2->N3
N3->N1

Where each node has a predefined number of edges allowed to connect to it. In this case that would be 2, no more, no less.

I could define a list of nodes and then a list of edges to create the network but I'm wondering if it is possible with F# to define my types in such a way that the compiler would throw an error if the graph was open or one of the nodes had edge connection <> 2?

I'm not sure this is possible to represent due to the restriction on cyclic dependencies.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this is close to what you want, although it doesn't enforce all the restrictions you mentioned:

type Node =
   {
      Name : string
      RefersTo : Node
   }

let rec graph =
   {
      Name = "N1"
      RefersTo =
         {
            Name = "N2"
            RefersTo =
                {
                    Name = "N3"
                    RefersTo = graph
                }
         }
   }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...