我有一個遞歸函數,我想在Mémoïsant記憶化列表ocaml的
我的遞歸函數重寫:
let rec sum_cube l =
match l with
| [] -> 0
| x :: s -> (x * x * x) + sum_cube s
,我試圖用這樣的:
let memo = Hashtbl.create 17
let rec sum_cub_memo l =
try
Hashtbl.find memo l
with Not_found ->
let fn = function
| [] -> 0
| x::s -> (x * x * x) sum_cub_memo s
in
Hashtbl.add memo l fn
fn ;;
我有一個錯誤:
此表達式的類型爲int list - > int但預計表達式爲int list !!
這體現在哪裏? – melpomene
啊。仔細看看'fn'。它是什麼?你怎麼使用它? – melpomene
@melpomene Hashtbl.add memo l fn – CamlX