2010-11-27 87 views
1

我想創建包含在OCaml的列表字符串的排列。 到目前爲止,我一直在處理以下代碼片段,但面臨着將列表的第一個字符串傳遞給我的方法的問題。錯誤 - 通過傳遞列表遞歸函數調用 - OCaml的

邏輯的代碼: 迭代到列表中的每個元素,並且每個元素與所述列表的元素追加。繼續執行,直到所有元素都被添加到列表中的每個可能位置。

代碼:

(* this function appends each string to each word in the list example: "A" with "ABC" *) 
let appendtocode n word = 
    let f x = n^x in 
    f word  
;; 

(* this function extracts every element of the list and appends it with the string. 
Example: "A" with ["AAA","ABC","ACD"] etc.. *) 
let appendtolist n list = 
    let f x = 
     if (List.length list) > 0 then list 
     else ((appendtocode n (List.hd list))^(appendtolist n (List.tl list))) 
    in 
    List.map f list 
;; 

錯誤:

我得到這個錯誤:

沒有限制值appendtolist

發生在調用:(appendtolistñList.tl名單)

我的列表只是由字符串組成。 我仍在處理代碼。但由於這個錯誤而停留在此處。

請幫忙!!!任何輸入都會很棒。

+0

這是爲什麼標籤SML和smlnj如果你使用ocaml的? – sepp2k 2010-11-27 23:40:04

+1

我修復了標籤,因爲這絕對是OCaml,與SML沒有明顯的聯繫。 – Porculus 2010-11-27 23:42:14

回答

2

要遞歸調用的函數,你需要用let rec appendtolist,而不是僅僅let appendtolist定義它。

然後你會得到一個不同的錯誤,因爲在你的代碼其他錯誤...

0

我不知道SML很好,但我認爲你需要更多的括號,例如

else ((append-to-code n List.hd list)^(append-to-list n List.tl list)) 

應該

else ((append-to-code n (List.hd list))^(append-to-list n (List.tl list))) 
1

因爲你遞歸調用appendtolist不宣遞歸你得到了「未綁定值appendtolist」的錯誤。

你需要寫let rec appendtolist n list = ...能夠參考appendtolist遞歸中它的定義。