【发布时间】:2016-01-17 00:48:16
【问题描述】:
我有这一段函数:
and interpret_read (id:string) (mem:memory)
(inp:string list) (outp:string list)
: bool * memory * string list * string list =
match inp with
| [] -> raise (Failure "unexpected end of input")
| head :: tail -> (true, (append mem (id, int_of_string head)), tail, outp)
内存类型定义如下:
type memory = (string * int) list;;
当我尝试#use 源代码时,我收到以下错误:
Error: This expression has type 'a * 'b
but an expression was epected of type (string * int) list
我还是 Ocaml 的新手,所以据我了解,'a 和 'b 是泛型类型,需要将它们定义为 string 和 int,然后才能将它们附加到 mem。我觉得这种理解并不完全准确,因为如果是这种情况,id 应该已经被定义为一个字符串,而 int_of_string 头应该是一个 int。谁能帮我解决我的困惑?
编辑: 我已将函数更改为以下内容:
and interpret_read (id:string) (mem:memory)
(inp:string list) (outp:string list)
: bool * memory * string list * string list =
match inp with
| [] -> raise (Failure "unexpected end of input")
| head :: tail -> (true, mem :: (id, int_of_string head), tail, outp)
我收到以下错误:
This expression has type memory = (string * int) list
but an expression was expected of type string * int
这对我来说没有意义,因为它应该是内存类型。
如果我将函数更改为以下内容:
and interpret_read (id:string) (mem:memory)
(inp:string list) (outp:string list)
: bool * memory * string list * string list =
match inp with
| [] -> raise (Failure "unexpected end of input")
| head :: tail -> (true, (id, int_of_string head), tail, outp)
然后我得到以下错误:
This expression has type 'a * 'b
but an expression was expected of type memory = (string * int) list
这就是表达式的类型!这里肯定有我遗漏的东西,但我无法弄清楚。
【问题讨论】:
标签: types pattern-matching ocaml