2
我想通過一個數組並返回一個int值列表(索引值),當數組中的值匹配true時。Ocaml從遞歸函數中返回一個列表
該數組是一個真/假值的布爾數組。
let get_elements (i:int)(b:bool) : int =
if b = true then (i::l)
else (())
;;
let rec true_list (b: bool array) : int list =
(fun i l -> get_elements i l)
;;
的語法是錯誤的我的代碼,我困惑於究竟是如何返回ints.I列表只想要回那些陣列中的真實元素的索引。
或用[電池](http://batteries.forge.ocamlcore.org/),'Array.fold_lefti(李樂趣IB - >若b的話,我::李李其他)[]' 。另外,我認爲在你的'ref'例子中,你應該真的把ref封裝在一個函數中,因爲在這裏它可能暗示ref始終是全局的,這比所需要的要難得多。 – gasche 2011-04-27 04:31:42
我很高興你改變了你的答案,不涉及引用。 – nlucaroni 2011-04-27 14:54:23