Tosh
Tosh

Reputation: 130

how to definie dataype tuple of lists of touples including lists in ocaml?

I am getting error when I define a type as: type table = (string * (string * string list) list) for encoding this value:

let atable = (
  "Student", 
  [
  ("Id", ["2";"4";"7";"9"]);
  ("Name", ["Jim";"Linnea";"Steve";"Hannah"]);
  ("Gender",["Male";"Female";"Male";"Female"]);
  ("Course",["Geography";"Economics";"Informatics";"Geography"
  ])

The environment I am using is this one as I had difficulties using cygwin in Windows.

How to define a datatype satisfying the data table student above?

Upvotes: 0

Views: 113

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66818

First, your expresion isn't syntactically valid. It's missing ]) at the end.

OCaml infers the types of things, so once you have a valid expression you can ask the toplevel for the type:

# let atable = (
  "Student",
  [
  ("Id", ["2";"4";"7";"9"]);
  ("Name", ["Jim";"Linnea";"Steve";"Hannah"]);
  ("Gender",["Male";"Female";"Male";"Female"]);
  ("Course",["Geography";"Economics";"Informatics";"Geography"
    ])]);;

val atable : string * (string * string list) list =
  ("Student",
   [("Id", ["2"; "4"; "7"; "9"]);
    ("Name", ["Jim"; "Linnea"; "Steve"; "Hannah"]);
    ("Gender", ["Male"; "Female"; "Male"; "Female"]);
    ("Course", ["Geography"; "Economics"; "Informatics"; "Geography"])])

So the type is indeed string * (string * string list) list.

If you still have a problem after fixing the syntax, the problem must be elsewhere. It would be helpful to see a full example along with the specific error you're seeing.

Upvotes: 2

Related Questions