0
我有一個函數的該段:OCaml的模式匹配類型的錯誤
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;;
當我嘗試#使用的源代碼我得到的以下錯誤:
Error: This expression has type 'a * 'b
but an expression was epected of type (string * int) list
我還是新來Ocaml程序編寫所以按照我的理解「和」 b是泛型類型,它們需要被定義d作爲字符串和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
這是什麼表達式的類型只是爲!這裏肯定有一些我不知道的東西,但我無法弄清楚。
是的,對不起,它是追加。那麼我怎麼附加一對呢? –
我想我會用「::」? –
我對OP做了一些改動,請看看是否有時間 –