Syntax 带有模块声明的general语句中出现OCaml语法错误

Syntax 带有模块声明的general语句中出现OCaml语法错误,syntax,ocaml,Syntax,Ocaml,请注意22和25之间的行 如果使用以下代码进行更改,会出现语法错误,但我不知道原因 1 module type Dictionary = sig 2 type ('k, 'v) t 3 4 val empty : ('k, 'v) t 5 val insert : 'k -> 'v -> ('k, 'v) t -> ('k, 'v) t 6 val lookup : 'k -> ('k, 'v) t -> 'v

请注意22和25之间的行
如果使用以下代码进行更改,会出现语法错误,但我不知道原因

  1 module type Dictionary = sig
  2     type ('k, 'v) t
  3
  4     val empty : ('k, 'v) t
  5     val insert : 'k -> 'v -> ('k, 'v) t -> ('k, 'v) t
  6     val lookup : 'k -> ('k, 'v) t -> 'v
  7 end
  8
  9 module DictionaryImpl : Dictionary = struct
 10     type ('k, 'v) t = ('k * 'v) list
 11     let empty = []
 12     let insert k v t = (k,v)::t
 13     (*let lookup k t = List.assoc k t*)
 14     let rec lookup k t =
 15         match t with
 16         | [] -> failwith "No data"
 17         | hd::tl ->
 18                if fst hd = k then snd hd
 19                else lookup k tl
 20 end
 21
 22 let _ =
 23     let a = DictionaryImpl.empty in
 24     let b = DictionaryImpl.insert "first" 1 a in
 25     DictionaryImpl.lookup "first" b
 26
我想的关键点是下划线语句是否存在。 我知道下划线的意思是忽略接收器变量,但我想知道在本文中我不知道的内容。

区别在于

let a = DictionaryImpl.empty in
let b = DictionaryImpl.insert "first" 1 a in
DictionaryImpl.lookup "first" b
是一个定义(即使此定义未绑定任何变量)。相反地

let _ =
  let a = DictionaryImpl.empty in
  let b = DictionaryImpl.insert "first" 1 a in
  DictionaryImpl.lookup "first" b
这是一个表达。 顶级表达式需要通过
;;与定义分开

let a = DictionaryImpl.empty in
let b = DictionaryImpl.insert "first" 1 a in
DictionaryImpl.lookup "first" b
然而,这种形式并不是完全惯用的,很多人宁愿放弃顶级定义而只使用定义。例如,一个人可以写

;; 
let a = DictionaryImpl.empty in
let b = DictionaryImpl.insert "first" 1 a in
DictionaryImpl.lookup "first" b
而不是

let location="universe"
let () = Format.printf "Hello, %s" location

你能分享确切的语法错误,包括它的行号和列号吗?一般来说,在询问时分享准确的错误是一个好规则:-)我可以问一下“顶级表达式”的确切含义吗?顶级表达式只是一个存在于任何定义之外的表达式。例如,
0
是模块M=struct 0 end
let location="universe"
;;
Format.printf "Hello, %s" location