2
對於一個賦值,我寫了下面的代碼遞歸。它需要一個矢量數據類型和一個矢量的列表,並計算兩個矢量的接近度。此方法工作正常,但我不知道如何做遞歸版本。Ocaml - 迭代遞歸
let romulus_iter (x:vector list) (vec:vector) =
let vector_close_hash = Hashtbl.create 10 in
let prevkey = ref 10000.0 in (* Define previous key to be a large value since we intially want to set closefactor to prev key*)
if List.length x = 0 then
{a=0.;b=0.}
else
begin
Hashtbl.clear vector_close_hash ;
for i = 0 to (List.length x)-1 do
let vecinquestion = {a=(List.nth x i).a;b=(List.nth x i).b} in
let closefactor = vec_close vecinquestion vec in
if(closefactor < !prevkey) then
begin
prevkey := closefactor;
Hashtbl.add vector_close_hash closefactor vecinquestion
end
done;
Hashtbl.find vector_close_hash !prevkey
end;;
任何幫助將非常感激
感謝的人,它最終點擊! – 2009-10-16 01:16:57