Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7699473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:29:12+00:00 2026-05-31T22:29:12+00:00

I am trying to represent a graph using disjoint union and record. The following

  • 0

I am trying to represent a graph using disjoint union and record. The following code cause a syntax error. How to define two variables when they refer to each other?

type 'a vertex = 
  |Empty
  |Vertex of 'a * 'a list;; (*a tuple consisting of any type of element and a list of vertex*);

let v0 = Vertex(0,[v1]) in
let v1=Vertex(1,[v0]);;

If I revise the code in to:

let rec v0 = Vertex(0,[v1]) and  v1=Vertex(1,[v0]);;

I will get v0:

[Vertex (1,
  [Vertex (0,
    [Vertex (1,
      [Vertex (0,
        [Vertex (1,
          [Vertex (0,
            [Vertex (1,
              [Vertex (0,
                [Vertex (1,
                  [Vertex (0,
                    [Vertex (1,
                      [Vertex (0,
                        [Vertex (1,
                          [Vertex (0,
                            [Vertex (1,
                              [Vertex (0,
                                [Vertex (1,
                                  [Vertex (0,
                                    [Vertex (1,
                                      [Vertex (0,
                                        [Vertex (1,
                                          [Vertex (0,
                                            [Vertex (1,
                                              [Vertex (0,
                                                [Vertex (1,
                                                  [Vertex (0,
                                                    [Vertex (1,
                                                      [Vertex (0,
                                                        [Vertex (1,
                                                          [Vertex (0,
                                                            [Vertex (1,
                                                              [Vertex (0,
                                                                [Vertex (1,
                                                                  [Vertex (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [Vertex
                                                                    (1,
                                                                    [Vertex
                                                                    (0,
                                                                    [...])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])]

This appears not what I want…

What is the best way to define a graph record containing a list or array of vertex? Obviously I cannot do this:

type graph = {
    vertex_set:array};;

I get the following message:

Error: The type constructor array expects 1 argument(s),
       but is here applied to 0 argument(s)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:29:13+00:00Added an answer on May 31, 2026 at 10:29 pm

    There is an error in your type definition. I’m pretty sure what you want is:

    type 'a vertex = 
      |Empty
      |Vertex of 'a * 'a vertex list
    

    You can define a value of this type using let rec:

    let rec v0 = Vertex(0, [v1]) and v1 = Vertex(1, [v0])
    

    In my experience it’s difficult to deal with values of this sort. OCaml is an eager language, so it’s difficult to compute with cyclic values.

    For the second question, the compiler is trying to tell you that array by itself isn’t a type. You need to say what it’s an array of.

    type 'a vertices = {
        vertex_set: 'a vertex array;
    }
    

    If you want a list of vertices rather than an array (probably a good idea), it would look like this:

    type 'a vertices = {
        vertex_set: 'a vertex list;
    }
    

    It seems to me you have two problems. First, you need to learn how to specify your types and values in OCaml. This isn’t so hard, and you can get good advice here. Second, you need to decide on what data structure to use to represent your graphs. This is harder, and there’s no overall best way to do it. You probably don’t want to use a data structure that is itself a graph (as above). More realistic possibilities are: an adjacency matrix, or a collection of vertices with lists of their outgoing edges. To avoid creating cyclic structures, you can just refer to vertices rather than including them directly in your edge lists. If the vertices were in an array, for example, you could refer to them by array index.

    There is a library named ocamlgraph that might be a better source of ideas than anything I can come up with!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to represent a two-dimensional coordinate grid with a two-dimensional array. Problem is,
Am trying to solve a labyrinth by DFS, using adj List to represent the
I am trying to represent a HD page in a struct. The code below
I'm trying to represent the following DTD fragment in XSD:- (A | B)* |
I am currently trying to represent a 2D array using tbb::concurrent_vector<T> . This 2d
I have a simple JSON object graph I'm trying to render using a Mustache
I am trying to represent the following situation, relating to the construction of a
I'm trying to create a 2D array to represent a weighted graph. To make
I am trying to define a graph with undirected edges from a set of
I'm trying to represent the following Perl line in PHP: $msg=!<connect_nettapi>\cD; # Message ends

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.