我在OCaml中爲一個學校項目實現了我自己的版本。它被定義爲這樣的:OCaml中的模式類型錯誤
type 'a my_list =
| Item of ('a * 'a my_list)
| Empty
;;
我的目標是實現從列表模塊20層的功能,並且第n是給了我很多的辛勤工作。它是一個遞歸函數,它也稱爲hd和長度函數。下面是代碼:
let rec length my_list =
match my_list with
| Empty -> 0
| Item (hd, tl) -> (length tl) + 1
;;
let hd my_list = function
| Empty -> raise (Failure "hd")
| Item (hd, tl) -> hd
;;
let rec nth my_list n =
let len = (length my_list) in
match my_list with
| lz when lz < 0 -> raise (Invalid_argument "nth")
| sup when n > len - 1 -> raise (Failure "nth")
| 0 -> (hd my_list)
| _ -> (nth my_list (n - 1))
;;
在編譯時,我得到這個錯誤:
$>ocamlc -w Aelz -warn-error A mylist.ml
File "mylist.ml", line 44, characters 10-11:
Error: This pattern matches values of type int
but a pattern was expected which matches values of type 'a my_list
參考以下行第n:| 0 -> (hd my_list)
什麼想法? 謝謝
編輯1:謝謝大家對你的wiseful答案,這裏是最後的代碼:
let rec nth my_list n =
if n < 0 then raise (Invalid_argument "nth") else
if my_list = Empty then raise (Failure "nth") else
if n = 0 then hd my_list else nth (tl my_list) (n-1)
;;
編輯2:感謝您的建議,這個人是更強類型:
let rec nth my_list n =
if n < 0 then raise (Invalid_argument "nth") else
match my_list with
| Empty -> raise (Failure "nth")
| Item (hd, tl) -> if n=0 then hd else
nth tl (n-1)
;;
你不應該在'len'而不是'my_list'上匹配嗎? – Marth
你是對的,謝謝! – bufferking
計算列表的長度不是要走的路:長度函數需要整個列表遍歷,並且在每一次迭代中調用它!你是否曾經用一個計數器遍歷列表的單個遍歷,從0開始,並且當計數器到達N時返回元素到你所在的位置? – ghilesZ