如何定义包括ocaml列表在内的列表列表的dataype元组?

时间:2018-04-29 02:08:18

标签: ocaml

当我将类型定义为:type table = (string * (string * string list) list)以对此值进行编码时,我收到错误:

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

我使用的环境是this one,因为我在Windows中使用cygwin时遇到了困难。

如何定义满足上述数据表学生的数据类型?

1 个答案:

答案 0 :(得分:2)

首先,你的表达在语法上是无效的。它最后缺少])

OCaml推断出事物的类型,所以一旦你有了一个有效的表达式,你就可以向顶层询问类型:

# 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"])])

所以类型确实是string * (string * string list) list

如果在修复语法后仍有问题,则问题必定在其他地方。看一个完整的例子以及你看到的具体错误会很有帮助。